我正在开发一个库存软件,在这个软件中,我希望通过将ProductName和ProductName与ProductCode进行比较,我已经存储在名为ProductLog的数据库表中,产品日志中的数据是:
ItemNO Productode ProductName ProductPrice
1 123 lux 58
2 321 soap 68现在,我只想在名为productCode的教科书中输入txtProductCode,然后按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);中,它说Input string was not in a correct format.
请帮我解决这个问题。
编辑:
我也尝试过这样的代码:
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'.意味着我在我的查询中调用数据库表时出错了,但是我无法发现这个错误.
发布于 2014-01-12 15:28:40
您的SQL有一些问题。
为了解决这个问题(第1-4点,我将把第5点留给你们自己研究),
sql = "";
sql = "SELECT dbo.ProductLog.ProductName, AND dbo.ProductLog.ProductName";
sql = " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";应该是
sql += "SELECT ProductName, ProductPrice";
sql += " FROM dbo.ProductLog";
sql += " WHERE ProductCode = '" + txtProdcutCode.Text + "'";注意:这个答案假设txtProductCode.Text的值是一个整数!
编辑:事实证明,ProductCode列是一个VarChar。对于OP和其他阅读此问题的人,当您收到SQL转换错误时,请检查SQL server中的列数据类型,并确保它与您提交的内容相匹配。
这是最基本的。还有很多其他的改进可以做,但这将使你前进。浏览一下基本的SQL语法,一旦你放下它,就可以让这个查询使用一个参数,而不是直接将txtProductCode.Text放在查询中。祝好运!
发布于 2014-01-12 15:08:20
永远不要调用InitializeComponent方法两次,它在创建表单和控件,在离开表单时调用表单的constructor.Probably,它再次创建,textBox将是blank.therefore,从代码中获取error.Delete InitializeComponent,然后再试一次。
更新:您的命令文本是wrong.here,应该使用+=
sql += " WHERE dbo.ProductLog.ProductCode = " + txtProdcutCode.Text + "";但是这并不优雅,safe.Instead这样使用参数化查询:
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 );https://stackoverflow.com/questions/21076056
复制相似问题