我正在尝试运行以下代码:
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
StringBuilder sqlStr = new StringBuilder("INSERT into Customers values ( @name, @address, @city, @state)");
SqlCommand cmd = new SqlCommand(sqlStr.ToString(), conn);
cmd.Parameters.Add(new SqlParameter("@name", "John Smith"));
cmd.Parameters.Add(new SqlParameter("@address", "123 Main St."));
cmd.Parameters.Add(new SqlParameter("@city", "Detroit"));
cmd.Parameters.Add(new SqlParameter("@state", "Michigan"));
cmd.ExecuteReader();
cmd.Parameters["@name"].Value = "William Jones";
cmd.Parameters["@address"].Value = "500 Blanchard Ave";
cmd.Parameters["@city"].Value = "Chicago";
cmd.Parameters["@state"].Value = "Illinois";
cmd.ExecuteReader();
}
但是,我收到一个错误。我尝试了多种其他方法,但似乎没有任何效果。使用SqlParameter
s 插入多条记录的正确方法是什么?