我发现system.data.sqlclient.sqlexception语法错误'错误消息
本文关键字:错误 消息 语法 sqlexception 发现 system data sqlclient | 更新日期: 2023-09-27 18:17:13
我的代码中出现了这个错误信息
system.data.sqlclient。'nvarchar'语法错误
必须声明标量变量@Flag
这是我到目前为止的代码:
try
{
if (dbConn.State == ConnectionState.Closed)
{
dbConn.Open();
}
dbCmd = new SqlCommand("INSERT INTO Shop(ShopID,ShopName,Location,Size,CostPerMeter,[TenantID(FK)],Flag)" +
"VALUES(@ShopID,@ShopName,@Location,@Size,@CostPerMeter,@[TenantID(FK)],@Flag);", dbConn);
dbCmd.Parameters.AddWithValue("@ShopID", txtPrimary.Text);
dbCmd.Parameters.AddWithValue("@ShopName", txtShopName.Text);
dbCmd.Parameters.AddWithValue("@Location", txtLoc.Text);
dbCmd.Parameters.AddWithValue("@Size", txtSize.Text);
dbCmd.Parameters.AddWithValue("@CostPerMeter", txtCPM.Text);
dbCmd.Parameters.AddWithValue("@[TenantID(FK)]", txtTenID.Text);
dbCmd.ExecuteNonQuery();
ds.Tables["Shop"].Rows.Add(new object[] { txtPrimary.Text, txtShopName.Text, txtLoc.Text, txtSize.Text, txtCPM.Text, txtTenID.Text, });
txtPrimary.Clear();
txtShopName.Clear();
txtLoc.Clear();
txtSize.Clear();
txtCPM.Clear();
txtTenID.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
您在查询中引用@Flag
,但从未为其提供相应的参数值。只需为@Flag
添加一个参数值:
dbCmd.Parameters.AddWithValue("@Flag", true); // set however appropriate