1

我是 c# 初学者,希望能在这里得到帮助。我没有使用 SQL Server,而只是在 c# 中输入了一个查询,如下所示:

SqlCommand custcmd = new SqlCommand("SELECT customer.customerid, warehouse.warehouseNo, warehouse.qty_goods, warehouse.qty_slack, warehouse.std_weight, (qty_goods+qty_slack)*std_weight/1000 AS Total 
FROM Customer 
INNER JOIN warehouse WHERE customer.customerid BETWEEN" + "textbox1.text" + "AND" + "textbox2.text", myconnection)

显示错误“WHERE 附近的语法不正确”有人可以评论和帮助吗?

预先感谢

4

2 回答 2

1
BETWEEN" + "textbox1.text" + "AND" + "textbox2.text"

如果您将 1 和 3 放在 textbox1 和 textbox2 中,它将产生

BETWEEN1AND3

所以这是一个错误

做:

BETWEEN " + "textbox1.text" + " AND " + "textbox2.text"

或更好:

string.Format("SELECT customer.customerid, warehouse.warehouseNo, warehouse.qty_goods, warehouse.qty_slack, warehouse.std_weight, (qty_goods+qty_slack)*std_weight/1000 AS Total 
FROM Customer INNER JOIN warehouse WHERE customer.customerid BETWEEN {0} AND {1}", textbox1.Text,textbox2.Text)

usingString.Format将防止您犯此类错误,因为您可以更好地看到将产生的查询

于 2013-10-24T06:35:39.467 回答
1

例如,在INNER JOIN warehouse您应该ON向链接表添加条件之后

INNER JOIN warehouse ON customer.<field> = warehouse.<field> 

这是错误“WHERE 附近语法不正确”的原因,但您也应该根据@wudzik 的回答更改 WHERE 语句

于 2013-10-24T06:38:41.277 回答