首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从数据库访问特定的数据字段

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

Stack Overflow用户
提问于 2014-01-12 14:54:02
回答 2查看 81关注 0票数 1

我正在开发一个库存软件,在这个软件中,我希望通过将ProductName和ProductName与ProductCode进行比较,我已经存储在名为ProductLog的数据库表中,产品日志中的数据是:

代码语言:javascript
复制
ItemNO       Productode       ProductName      ProductPrice
     1             123              lux              58
     2             321              soap             68

现在,我只想在名为productCode的教科书中输入txtProductCode,然后按tab键,然后ProductPrice(txtProductPrice)和ProductName(txtProductName)框自动填充。

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

代码语言:javascript
复制
  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);中,它说Input string was not in a correct format.

请帮我解决这个问题。

编辑:

我也尝试过这样的代码:

代码语言:javascript
复制
   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();




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

但是它说Incorrect syntax near the keyword 'WHERE'.意味着我在我的查询中调用数据库表时出错了,但是我无法发现这个错误.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-12 15:28:40

您的SQL有一些问题。

  1. 您最初是在重写sql变量,最后只使用WHERE子句;
  2. 您没有from语句,因此数据库不知道从哪里检索记录。
  3. SELECT语句中AND的使用是不正确的;您只需要逗号来分隔字段。
  4. 您永远不会从DB中选择ProductPrice,而是选择两次ProductName!
  5. 您没有在查询中使用参数化SQL,使您的应用程序容易受到SQL注入攻击。

为了解决这个问题(第1-4点,我将把第5点留给你们自己研究),

代码语言:javascript
复制
  sql = "";
  sql = "SELECT dbo.ProductLog.ProductName, AND dbo.ProductLog.ProductName";
  sql = " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";

应该是

代码语言:javascript
复制
  sql += "SELECT ProductName, ProductPrice";
  sql += "  FROM dbo.ProductLog";
  sql += " WHERE ProductCode = '" + txtProdcutCode.Text + "'";

注意:这个答案假设txtProductCode.Text的值是一个整数!

编辑:事实证明,ProductCode列是一个VarChar。对于OP和其他阅读此问题的人,当您收到SQL转换错误时,请检查SQL server中的列数据类型,并确保它与您提交的内容相匹配。

这是最基本的。还有很多其他的改进可以做,但这将使你前进。浏览一下基本的SQL语法,一旦你放下它,就可以让这个查询使用一个参数,而不是直接将txtProductCode.Text放在查询中。祝好运!

票数 2
EN

Stack Overflow用户

发布于 2014-01-12 15:08:20

永远不要调用InitializeComponent方法两次,它在创建表单和控件,在离开表单时调用表单的constructor.Probably,它再次创建,textBox将是blank.therefore,从代码中获取error.Delete InitializeComponent,然后再试一次。

更新:您的命令文本是wrong.here,应该使用+=

代码语言:javascript
复制
sql += " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";

但是这并不优雅,safe.Instead这样使用参数化查询

代码语言:javascript
复制
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 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21076056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档