CountClass countClass=new CountClass ();
//类
{
/// <returns>返回国籍名与ID</returns>
public static ArrayList getCountryArrayList(string cname)
{
ArrayList arraylist = new ArrayList();
string sql = "select C_ID ,c_namee from country where c_namee like '%" + cname + "%'";
SqlConnection con = new SqlConnection(SqlHelper.SQLConnectionString);
SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.Text, sql, null);
while (dr.Read())
{
arraylist.Add(dr["C_ID"].ToString() + "," + dr["c_namee"].ToString());
}
return arraylist;
}
/// <returns>返回国籍名与ID</returns>
public static string getCountryID(string cname)
{
string countryid = string.Empty;
string sql = "select C_ID from country where c_namee ='" + cname + "'";
SqlConnection con = new SqlConnection(SqlHelper.SQLConnectionString);
SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.Text, sql);
while (dr.Read())
{
countryid = dr[0].ToString();
}
dr.Close();
return countryid;
}
}
///页面事件
private void txtCountry_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if (e.KeyChar == (char)Keys.Enter)
{
if (lstCountry != null)
{
if (lstCountry.Items.Count != 0)
{
lstCountry.Items.Clear();
}
}
ArrayList arraylist = countClass.getCountryArrayList(this.txtCountry.Text);
if (arraylist != null)
{
if (arraylist.Count != 0)
{
for (int i = 0; i < arraylist.Count; i++)
{
lstCountry.Items.Add((arraylist[i].ToString().Split(','))[1].ToString());
}
this.lstCountry.Visible = true;
}
else
{
MessageBox.Show("没有这个国家名称,请重新录入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtCountry.Focus();
this.txtCountry.Text = "";
this.lstCountry.Visible = false;
}
}
}
}
catch (Exception ex)
{
ex.GetBaseException();
}
}
private void lstCountry_DoubleClick(object sender, EventArgs e)
{
if (lstCountry.Items.Count != 0)
{
this.txtCountry.Text = this.lstCountry.Text;
countryidlist = countClass.getCountryID(this.txtCountry.Text);
this.lstCountry.Visible = false;
}
}
本文介绍了一个简单的国家查询功能实现方式,通过输入国家名称,系统能够返回对应的国家ID及名称列表。利用C#语言实现了数据库查询操作,并展示了如何响应用户输入进行实时反馈。

被折叠的 条评论
为什么被折叠?



