C# C“SQL插入语句”;重复变量";
下面的代码应该遍历组件列表,并为特定的BOM表插入它们。代码执行循环一次,然后发送错误消息 变量名“@product”已声明。变量名 必须在查询批处理或存储过程中取消查询 我不知道最好的方式来改变这一点,或者为什么这是抛出。有什么想法吗 我是否需要使用语句将foreach移到第二个C# C“SQL插入语句”;重复变量";,c#,sql-server,C#,Sql Server,下面的代码应该遍历组件列表,并为特定的BOM表插入它们。代码执行循环一次,然后发送错误消息 变量名“@product”已声明。变量名 必须在查询批处理或存储过程中取消查询 我不知道最好的方式来改变这一点,或者为什么这是抛出。有什么想法吗 我是否需要使用语句将foreach移到第二个之外 public static void insert_rework(string product, List<string> component_list, string sn, DateTime to
之外
public static void insert_rework(string product, List<string> component_list, string sn, DateTime today, string connectionString)
{
string sql =
@"INSERT INTO table (
Product,
Component,
Serial_Num,
Cur_Date)
VALUES (
@product,
@component,
@sn,
@date)";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
foreach (string component in component_list)
{
command.Parameters.AddWithValue("@product", product);
command.Parameters.AddWithValue("@component", component);
command.Parameters.AddWithValue("@sn", sn);
command.Parameters.AddWithValue("@date", today);
try
{
command.ExecuteNonQuery();
}
catch (SqlException sq)
{
Console.WriteLine(sq.Message);
}
}
}
}
public static void insert\u rework(字符串产品、列表组件、字符串序列号、当前日期、字符串连接字符串)
{
字符串sql=
@“插入到表中(
产品,,
组成部分,
序列号,
当前日期)
价值观(
@产品,,
@组成部分,
@sn,
@"日期";;
使用(SqlConnection连接=新的SqlConnection(connectionString))
使用(SqlCommand=newsqlcommand(sql,连接))
{
connection.Open();
foreach(组件列表中的字符串组件)
{
command.Parameters.AddWithValue(“@product”,product);
command.Parameters.AddWithValue(“@component”,component);
command.Parameters.AddWithValue(“@sn”,sn);
command.Parameters.AddWithValue(“@date”,今天);
尝试
{
command.ExecuteNonQuery();
}
捕获(SqlException sq)
{
控制台写入线(sq.Message);
}
}
}
}
在foreach循环中有AddWithValue调用。当然,这意味着在每个循环中,您都读取了具有不同值的相同参数
在循环外部声明它们,然后在循环内部更改值怎么样
command.Parameters.Add("@product", SqlDbType.NVarChar);
command.Parameters.Add("@component", SqlDbType.NVarChar);
command.Parameters.Add("@sn", SqlDbType.NVarChar);
command.Parameters.Add("@date", SqlDbType.Date);
foreach (string component in component_list)
{
command.Parameters["@product"].Value = product;
command.Parameters["@component"].Value = component;
command.Parameters["@sn"].Value = sn;
command.Parameters["@date"].Value = today;
.....
当然,您可以使用Clear从集合中删除任何参数,但这似乎是不必要的。此外,某些值在循环中似乎永远不会更改。这些值可以在循环外添加一次
command.Parameters.Add("@product", SqlDbType.NVarChar);
command.Parameters.Add("@component", SqlDbType.NVarChar).Value = component;
command.Parameters.Add("@sn", SqlDbType.NVarChar).Value = sn;
command.Parameters.Add("@date", SqlDbType.Date).Value = today;
foreach (string component in component_list)
{
command.Parameters["@component"].Value = component;
command.ExecuteNonQuery();
}
还请注意,您应该始终使用Add而不是AddWithValue,因为此方法虽然方便,但众所周知会影响性能,如果传递给AddWithValue的值不是基础表所期望的类型,有时它也会给出错误的结果。(本地化字符串而不是日期、本地化字符串而不是小数、双精度等)每次在foreach循环中添加相同的参数。在添加参数之前清除参数,或检查是否存在并执行相应操作:
if (command.Parameters.Contains("@product"))
command.Parameters["@product"].Value = product;
else
command.Parameters.AddWithValue("@product", product);
在循环开始时调用Parameters Clear(),这是否回答了您的问题?感谢您的回答,我了解到command.Parameters的添加功能已被弃用。这是真的吗?不推荐使用带有签名(parametername,object)的版本,但不推荐使用带有SqlDbTypeMore info:和