C#2008 联接数据库查找某货品查找请教!
 添加一个textBox5 在文本框中录入货品编码后点击button4 查找出该货品编码的相关编码
请问这个如何实现,谢谢!
        private void button4_Click(object sender, EventArgs e)
        {
            string ConnectString = @"server=localhost;uid=sa;pwd=sankyqiu;database=yqerp2008";
            SqlConnection conn = new SqlConnection(ConnectString);
            SqlDataAdapter adapter;
            DataSet ds;            try
            {
                conn.Open();
                adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods ", conn);                ds = new DataSet();
                adapter.Fill(ds);
                EmpGridView.DataSource = ds.Tables[0].DefaultView;
            }
            finally
            {
                conn.Close();
            }
        }

解决方案 »

  1.   

          private void button4_Click(object sender, EventArgs e) 
            { 
                string ConnectString = @"server=(local);uid=sa;pwd=sankyqiu;database=yqerp2008"; 
                SqlConnection conn = new SqlConnection(ConnectString); 
                SqlDataAdapter adapter; 
                DataSet ds;             try 
                { 
                    conn.Open(); 
                    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods extwhere goodsid = "+textbox5.text+"", conn);                 ds = new DataSet(); 
                    adapter.Fill(ds); 
                    EmpGridView.DataSource = ds.Tables[0].DefaultView; 
                } 
                finally 
                { 
                    conn.Close(); 
                } 
            }
      

  2.   

    谢谢楼上的教导
    ("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods extwhere goodsid = "+textbox5.text+"", conn); 
    是不是改为以下呀,(textBox5.Text 分大小写)
    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods where code = "+textBox5.Text+"", conn);
    但执行有如下提示:
    将 varchar值'****'转换类型为int列时发生语法错误.
      

  3.   

    应该是你输入到  textbox5中的值 有不能转换为 数值型的
    比如输入了字母 或者其他非数字的字符
    貌似你数据库里 code 字段是数值型的
      

  4.   

    试试adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods where code = "+Convert.Toint32(textBox5.Text)+"", conn); 
      

  5.   


    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods where code = '+Convert.Toint32(textBox5.Text)+'", conn); 
    是不是" ' 的问题?
      

  6.   

    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from goods where code = '"+Convert.Toint32(textBox5.Text)+"'", conn); where code='textbox5.text的数值'你没写'';
    如果再报错那就是你数据库中code字段的数据类型与你textbox5输入的数据字段,类型不匹配了。
      

  7.   

    谢谢回复,
    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from l_goods where code = '"+Convert.ToInt32(textBox5.Text)+" ' ", conn);这样就可通过运行,但是
    如果在textBox5 里录入货品编码 009 后点击按钮却没显示出来!
      

  8.   

    你数据库中Code是什么类型的,如果是字符型的,应该是吧,不然不可能存有009,这样的话你就不需要做Convert.ToInt32(textBox5.Text)这个转换,一转化把009前面的00都去掉了,肯定就不匹配了
      

  9.   

    adapter = new SqlDataAdapter("select goodsid as 货品ID,code AS 货品编码,name AS 货品名称,spec AS 货品规格 from l_goods where code = '"+textBox5.Text+"'", conn); 
      

  10.   

    你数据库中的goodsid 是int类型,但TextBox中传入的值却是字符串类型,导致类型不匹配,你可以尝试用正则式规定TextBox必须输入数字类型