C# 如何从数据库访问数据的特定字段

C# 如何从数据库访问数据的特定字段,c#,mysql,.net,database,C#,Mysql,.net,Database,我正在使用一个清单软件,在该软件中,我希望通过将产品名称和产品价格与ProductCode(我已存储在名为ProductLog的数据库表中的数据)进行比较来访问产品名称和产品价格,产品日志中的数据为: ItemNO Productode ProductName ProductPrice 1 123 lux 58 2 321

我正在使用一个清单软件,在该软件中,我希望通过将产品名称和产品价格与ProductCode(我已存储在名为ProductLog的数据库表中的数据)进行比较来访问产品名称和产品价格,产品日志中的数据为:

ItemNO       Productode       ProductName      ProductPrice
     1             123              lux              58
     2             321              soap             68
现在我只想在名为txtProductCode的教科书中输入productCode,然后按tab键,ProductPrice(txtProductPrice)和ProductName(txtProductName)框将自动填充

我尝试比较Productcode和access值的代码是:

  private void txtProdcutCode_Leave(object sender, EventArgs e)
        {
            ///////////////////////////////////////////////////////////////////////

            InitializeComponent();
            string sql;

            int productCode = 0;

            productCode = Convert.ToInt32(txtProdcutCode.Text);

            sql = "";
        sql = "SELECT dbo.ProductLog.ProductName, dbo.ProductLog.ProductName";
        sql = " WHERE ProductLog.ProductCode = " + txtProdcutCode.Text + "";


            SqlConnection cn = new SqlConnection();
            SqlCommand rs = new SqlCommand();
            SqlDataReader sdr = null;
            clsConnection clsCon = new clsConnection();


            clsCon.fnc_ConnectToDB(ref cn);


            rs.Connection = cn;
            rs.CommandText = sql;
            sdr = rs.ExecuteReader();


            while (sdr.Read())
            {

                txtProductPrice.Text = sdr["ProductPrice"].ToString();
                txtProductName.Text = sdr["ProductName"].ToString();
            }


            //lblTotalQuestion.Text = intQNo.ToString();

            sdr.Close();
            rs = null;
            cn.Close();




            /////////////////////////////////////////////////////////////////////////         
        }
但是在第行
productCode=Convert.ToInt32(txtProdcutCode.Text)表示输入字符串的格式不正确。

请帮我解决这个问题

编辑: 我还尝试了以下代码:

   private void txtProdcutCode_Leave(object sender, EventArgs e)
        {
            ///////////////////////////////////////////////////////////////////////

            string sql;

          //  int productCode = 0;

            //productCode = Convert.ToInt32(txtProdcutCode.Text);

            sql = "";
            sql = "SELECT dbo.ProductLog.ProductName, AND dbo.ProductLog.ProductName";
            sql = " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";


            SqlConnection cn = new SqlConnection();
            SqlCommand rs = new SqlCommand();
            SqlDataReader sdr = null;
            clsConnection clsCon = new clsConnection();


            clsCon.fnc_ConnectToDB(ref cn);


            rs.Connection = cn;
            rs.CommandText = sql;
            sdr = rs.ExecuteReader();


            while (sdr.Read())
            {

                txtProductPrice.Text = sdr["ProductPrice"].ToString();
                txtProductName.Text = sdr["ProductName"].ToString();
            }


            //lblTotalQuestion.Text = intQNo.ToString();

            sdr.Close();
            rs = null;
            cn.Close();




            /////////////////////////////////////////////////////////////////////////         
        }

但是它说,
关键字“WHERE”附近的语法不正确。
表示我在查询中调用数据库表时出错,但我无法找出错误…

永远不要调用
InitializeComponent
方法两次。它正在创建表单和控件,并调用表单的构造函数。可能当您离开文本框时,它会再次创建,而
文本框将为空。因此,您会收到该错误。请删除
从代码中初始化组件
,然后重试

更新:您的命令文本错误。您应该在此处使用
+=

sql += " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";
但这不是优雅和安全的。而是像这样使用:

SqlCommand cmd = new SqlCommand();
cmd.Connection =  cn;
cmd.CommandText = "SELECT dbo.ProductLog.ProductName,dbo.ProductLog.ProductName WHERE dbo.ProductLog.ProductCode = @pCode";
cmd.Parameters.AddWithValue("@pCode", txtProdcutCode.Text );

您的SQL存在一些问题

  • 您最初覆盖了sql变量,结果只得到了WHERE子句
  • 您没有FROM语句,因此数据库不知道您试图从何处检索记录
  • SELECT语句中使用AND是不正确的;您只需要逗号来分隔字段
  • 您永远不会从DB中选择ProductPrice,而是选择两次ProductName
  • 您没有在查询中使用参数化SQL,使您的应用程序容易受到SQL注入攻击
  • 为了解决这个问题(第1-4点,我将第5点留给您自己的研究)

    应该是

      sql += "SELECT ProductName, ProductPrice";
      sql += "  FROM dbo.ProductLog";
      sql += " WHERE ProductCode = '" + txtProdcutCode.Text + "'";
    
    注意:此答案假设txtProductCode.Text的值是一个整数

    编辑:原来列ProductCode是一个VarChar。用于OP 以及其他阅读此问题的人,当您遇到SQL转换错误时 检查SQL server中的列数据类型,确保它与 你在屈服


    这是最基本的。还有许多其他的改进可以做,但这将使你去。温习一下基本的SQL语法,一旦掌握了基本语法,就要考虑让这个查询使用一个参数,而不是直接将
    txtProductCode.Text
    放入查询中。祝你好运

    确切地说,
    txtProdcutCode.Text
    的值是多少?它在txtProductCode.Text中是123。为什么在leave事件中调用
    InitializeComponent
    ?该行显示:sql=“”;接下来的两行应该以sql+=“我要做一个回答:)你仍然有一个AND语句是从他最初的SELECT语句复制而来的,而no语句是从SELECT语句复制而来的;那样不行。他的SQL太糟糕了!将varchar值“abc”转换为int数据类型时转换失败。当您知道“abc”必须是int时,为什么要将其放在文本框中?!这是在构建SQL语句之前进行验证的工作。即使我将123添加到ProductCode中,听起来也像是浏览器缓存问题。或者您没有在每次回发时检索文本框的值。要测试我的代码并隔离其他不相关的问题,可以在SQL语句“txtProductCode.Text=“123”之前添加一行,这样就可以了。这只是为了测试/验证我的代码,然后取出它并解决您的其他问题。您真是一个好人,兄弟:)帮了很多忙:)初学者的一个好例子:)再次感谢:)
      sql += "SELECT ProductName, ProductPrice";
      sql += "  FROM dbo.ProductLog";
      sql += " WHERE ProductCode = '" + txtProdcutCode.Text + "'";