為什么這在我使用絕對值時有效,但是當我嘗試使用變數作為查詢引數時,查詢不回傳資料?
private void SetBomNumber()
{
try
{
da2 = new SqlDataAdapter("SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) = '210002')) ", Cn);
da2.Fill(dt2);
if (dt2.Rows.Count > 0)
{
int BomNumber= Convert.ToInt32(dt2.Rows[0]["NewBomSerial"].ToString());
txtBomNum.Text = BomNumber.ToString();
MessageBox.Show("The Next Serial Is :" BomNumber);
}
else
{
MessageBox.Show("The Query Doesn’t Work");
}
}
catch (Exception Err)
{
MessageBox.Show("This Error Occured :" Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
使用變數,這會顯示錯誤訊息“查詢不起作用”?
private void SetBomNumber()
{
try
{
da2 = new SqlDataAdapter("SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) = @prcode)) ", Cn);
da2.SelectCommand.Parameters.AddWithValue("@prcode", "%" txtprcode.Text "%");
da2.Fill(dt2);
if (dt2.Rows.Count > 0)
{
int BomNumber= Convert.ToInt32(dt2.Rows[0]["NewBomSerial"].ToString());
txtBomNum.Text = BomNumber.ToString();
MessageBox.Show("The Next Serial Is :" BomNumber);
}
else
{
MessageBox.Show("The Query Doesn’t Work");
}
}
catch (Exception Err)
{
MessageBox.Show("This Error Occured :" Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
此代碼以其他形式作業沒有問題,在此先感謝。
uj5u.com熱心網友回復:
基于@DRapp的評論感謝他,這對我有用:如果您需要條件是變數的確切值,請使用:
da2 = new SqlDataAdapter("SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) = @prcode)) ", Cn);
da2.SelectCommand.Parameters.AddWithValue("@prcode", txtprcode.Text );
如果您需要條件是 Like 變數,您可以使用:
da2 = new SqlDataAdapter("SELECT Products.[ProductCode], Max(ISNULL([BomNumber], 0) 1) AS NewBomSerial FROM Products LEFT JOIN Bom ON Products.ProductCode = Bom.ProductCode GROUP BY Products.[ProductCode] HAVING(((Products.[ProductCode]) LIKE @prcode)) ", Cn);
da2.SelectCommand.Parameters.AddWithValue("@prcode", "%" txtprcode.Text "%");
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/469302.html
