TextBox里录入的信息相当于Combox

本文介绍了一个简单的国家查询功能实现方式,通过输入国家名称,系统能够返回对应的国家ID及名称列表。利用C#语言实现了数据库查询操作,并展示了如何响应用户输入进行实时反馈。

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;
                }
            }  

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CPU_2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值