1

I am trying to run the following code:

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

However, I am getting an error. I have tried multiple other ways, but nothing seems to work. What is the proper way to insert multiple records using SqlParameters?

3
  • Get rid of the @ and you should be fine. Commented Apr 5, 2013 at 3:47
  • @code4life: that's definitely not the problem ... SQL Server parameter typically do have a leading @ Commented Apr 5, 2013 at 5:33
  • Ugh, my bad - I should rephrase. How about removing the @ in the cmd.Parameters["@name"].Value? Commented Apr 5, 2013 at 12:34

4 Answers 4

4

First of all cmd.ExecuteReader(); is used on data retrieval and not not the proper way in doing insert.

Use

cmd.ExecuteNonQuery();

Change your code to this:

Create a method that has 4 parameter where you pass name,address,city and state that includes your insert code

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

                }

}

then on your click or any events that you want to insert new customer just do

InsertCustomers("A","B","C","D");

InsertCustomers("E","F","G","H");

And maybe you need to start learning the basic first on ADO.NET

The C# Station ADO.NET Tutorial

After completing the tutorial, you will now be familiarize on ado.net.

Best Regards

Sign up to request clarification or add additional context in comments.

3 Comments

Oh wow, I didn't even notice that. I've learned all the ADO basics, but thanks for the input.
Welcome...please accept as answer if my post help you..thanks
The using(SqlConnection ...) { ... } approach is good and advisable - but you should also use this on SqlCommand!
1

Alter your code

put AddWithValue instead of Add

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

and ExecuteNonQuery() instead of ExecuteReader();

cmd.ExecuteNonQuery();

Comments

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

Comments

0

I used to just keep the reference to the SqlParameter object and reuse it, like this:

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

This should resolve your problems now. As an added benefit, this also eliminates the collection lookup that you were doing:

cmd.Parameters["blah"]

And replaces it with direct access to the parameter you need. I think this should save a few cycles in the clock (not much though, lol).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.