c# – cmd.Parameters.AddWithValue(“@param1”,param1 ?? DbNull.Value) – Stack Overflow

Cmd.parameters.addwithvalue("@param1",param1 ?? dbnull.value)

I would believe that changing the line to the following should work:

cmd.Parameters.AddWithValue("@col_1", val1 ?? DBNull.Value);

Since this would set the parameter value to an object type.

If that fails for some reason try to cast the result to an object like this:

cmd.Parameters.AddWithValue("@col_1", (object)(val1 ?? DBNull.Value));

But this should not be necessary.

Actually, this should work:

cmd.Parameters.AddWithValue("@col_1", val1 ?? (object)DBNull.Value);

How to add multiple parameters to sql command in one statement?

As far as I know, your code is the most compact possible in term of lines count, however you could use the List<SqlParameter> with the object initializer syntax to have just one line terminated by a semicolon to build your parameter list, then pass that list as the array of parameters expected by the AddRange method

 List<SqlParameter> prm = new List<SqlParameter>()
     new SqlParameter("@variable1", SqlDbType.Int) {Value = myValue1},
     new SqlParameter("@variable2", SqlDbType.NVarChar) {Value = myValue2},
     new SqlParameter("@variable3", SqlDbType.DateTime) {Value = myValue3},

Notice that with this approach you need to define correctly the datatype of the parameter. In my example I have used some arbitrary types to show the correct syntax

A bit off-topic, by I think that in this general context is interesting to point out that AddWithValue is not to be considered when you want to get the best performance possible.

In this article on MSDN How data access code affects database perfomance is well explained why one should avoid the AddWithValue method for performance reasons.
In short, using AddWithValue could be a problem for the Sql Server Optimizer because the parameters of type string are passed with the size equal to the current length of the string. But this will force the Sql Server Optimizer to discard the query plan created for a previous identical call but with a string of different length.
It is better to call the SqlParameter constructor specifying the type and the size of the parameter and don’t worry how to compress the size of the calls.

:/>  Разбить диск на разделы Windows 10 — 7 во время установки — пошаг

Insert into c# with sqlcommand

What’s the best way to INSERT data into a database?

This is what I have but it’s wrong..

cmd.CommandText = "INSERT INTO klant(klant_id,naam,voornaam) VALUES(@param1,@param2,@param3)";

cmd.Parameters.Add(new SqlParameter("@param1", klantId));
cmd.Parameters.Add(new SqlParameter("@param2", klantNaam));
cmd.Parameters.Add(new SqlParameter("@param3", klantVoornaam));

The function add data into the listBox

http://www.pictourl.com/viewer/37e4edcf(link is dead)

but not into the database..

http://www.pictourl.com/viewer/4d5721fc(link is dead)

The full function:

private void Form1_Load(object sender, EventArgs e)
    conn2 = new SqlConnection();
    conn2.ConnectionString = ConfigurationManager.ConnectionStrings["connSpionshopString"].ConnectionString;

private void button2_Click(object sender, EventArgs e)
    string sqlCmd = "SELECT naam,voornaam,klant_id FROM klant;";
    SqlCommand cmd = new SqlCommand(sqlCmd, conn2);


    using(SqlDataReader reader = cmd.ExecuteReader())
        while (reader.Read())
            listBox2.Items.Add(reader.GetString(0)   " "   reader.GetString(1)   "  ("   reader.GetInt16(2)   ")");

private void button4_Click(object sender, EventArgs e)
    int klantId = Convert.ToInt32(textBox1.Text);
    string klantNaam = textBox2.Text;
    string klantVoornaam = textBox3.Text;


    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn2;
    cmd.CommandText = "INSERT INTO klant(klant_id, naam, voornaam)   VALUES(@param1,@param2,@param3)";

    cmd.Parameters.AddWithValue("@param1", klantId);
    cmd.Parameters.AddWithValue("@param2", klantNaam);
    cmd.Parameters.AddWithValue("@param3", klantVoornaam);



Using sqlcommand , how to add multiple parameters to its object , insertion via winform in sql table

The 2 ‘solutions’ that you suggest in your question, are semantically different.
Which one you should use, depends on your table-layout.

The first solution inserts one record in the table, the second insert statement inserts one record (row) for every value (textbox).

Difficult to give a good answer here, since we do not know what you’re going to save in that table, and thus , we cannot say how you should save it (how you save it, is inherintly dependent on the way you should call the SQL insert statement).

Оставьте комментарий