我正在尝试运行以下代码:
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
答案 0 :(得分:4)
首先,cmd.ExecuteReader();
用于数据检索,而不是正确的插入方式。
使用
cmd.ExecuteNonQuery();
将您的代码更改为:
创建一个具有4个参数的方法,您可以在其中传递包含插入代码
的名称,地址,城市和州private void InsertCustomers(string name,string address,string city,string state)
{
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", name));
cmd.Parameters.Add(new SqlParameter("@address", address));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@state", state));
cmd.ExecuteNonQuery();
}
}
然后在您的点击或您要插入新客户的任何事件上执行
InsertCustomers("A","B","C","D");
InsertCustomers("E","F","G","H");
也许你需要先在ADO.NET上学习基础知识
The C# Station ADO.NET Tutorial
完成本教程后,您将熟悉ado.net。
最好的问候
答案 1 :(得分:1)
更改您的代码
将 AddWithValue 代替添加
cmd.Parameters.AddWithValue(new SqlParameter("name", "John Smith"));
和 ExecuteNonQuery()而不是ExecuteReader();
cmd.ExecuteNonQuery();
答案 2 :(得分:0)
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"));
int rowsAffected = cmd.ExecuteNonQuery();
cmd.Parameters["name"].Value = "William Jones";
cmd.Parameters["address"].Value = "500 Blanchard Ave";
cmd.Parameters["city"].Value = "Chicago";
cmd.Parameters["state"].Value = "Illinois";
rowsAffected = cmd.ExecuteNonQuery();
}
答案 3 :(得分:0)
我过去只保留对SqlParameter
对象的引用并重用它,如下所示:
var paramName = new SqlParameter("@name", "John Smith");
var paramAddress = new SqlParameter("@address", "123 Main St.");
var paramCity = new SqlParameter("@city", "Detroit");
var paramState = new SqlParameter("@state", "Michigan");
SqlCommand cmd = new SqlCommand(sqlStr.ToString(), conn);
cmd.Parameters.AddRange(new[]{paramName, paramAddress, paramCity, paramState});
cmd.ExecuteReader();
paramName.Value = "William Jones";
paramAddress.Value = "500 Blanchard Ave";
paramCity.Value = "Chicago";
paramState.Value = "Illinois";
cmd.ExecuteReader();
这应该可以解决您现在的问题。作为额外的好处,这也消除了您正在进行的集合查找:
cmd.Parameters["blah"]
并替换它直接访问您需要的参数。我认为这应该在时钟中保存几个周期(虽然不多,哈哈)。