1

我正在尝试运行以下代码:

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();
}

但是,我收到一个错误。我尝试了多种其他方法,但似乎没有任何效果。使用SqlParameters 插入多条记录的正确方法是什么?

4

4 回答 4

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 上开始学习基础知识

C# Station ADO.NET 教程

完成本教程后,您现在将熟悉 ado.net。

此致

于 2013-04-05T03:03:58.907 回答
1

更改您的代码

AddWithValue而不是 Add

cmd.Parameters.AddWithValue(new SqlParameter("name", "John Smith"));

ExecuteNonQuery()而不是 ExecuteReader();

cmd.ExecuteNonQuery();
于 2013-04-05T10:38:00.130 回答
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();
            }
于 2013-04-05T03:06:51.807 回答
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"]

并将其替换为直接访问您需要的参数。我认为这应该在时钟中节省几个周期(虽然不多,哈哈)。

于 2013-04-05T12:40:00.953 回答