我是C#的新手并且可以使用一些指导,谷歌搜索已经建议了许多不同的东西,但没有一个我可以开始工作.我已经阻止了数据源详细信息等,但这些在代码中是有效的
当我点击"保存按钮"时,下面是我的整个代码
public void testLoadAuditBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=XXXXX; Initial Catalog=XXXXX; User ID=XXXXX;Password=XXXXX");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT INTO testloadaudit (item_type,load_id,UserId, Comment, dateamended) VALUES (@item_type, @load_id, @UserId, @Comment, @dateamended)";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误
System.Data.dll中发生类型为"System.Data.SqlClient.SqlException"的未处理异常附加信息:必须声明标量变量"@item_type".
如果有人可以建议如何将上面的插入物放入表中那将是很好的.
您已在查询文本中使用了参数,但尚未将它们添加到SqlCommand.
在执行查询之前,您需要为每个参数添加这样的代码:
cmd.Parameters.Add("@item_type", SqlDbType.????);
cmd.Parameters["@item_Type"].Value = ???
Run Code Online (Sandbox Code Playgroud)
您应该为每个参数指定适当的类型和值.
| 归档时间: |
|
| 查看次数: |
1145 次 |
| 最近记录: |