1

I have ten textboxes in my winform, and i need to save the text typed in these textboxes into 10 columns of a sql database table. so, for this shall i write :

INSERT INTO item (c1,c2,c3...,c10) values (@a,@b....@j) 

cmd.Parameters.Add("@a",SqlDbType.Varchar)
cmd.Parameteres["@a"].Value=textbox1.Text;

cmd.Parameters.Add("@b",SqlDbType.Varchar)
cmd.Parameteres["@b"].Value=textbox2.Text;.
.
.
.
.
cmd.Parameters.Add("@j",SqlDbType.Varchar)
cmd.Parameteres["@j"].Value=textbox10.Text;

OR ten separate queries for each textbox:

INSERT INTO item (c1) values (@a)
cmd.Parameters.Add("@a",SqlDbType.Varchar)
cmd.Parameteres["@a"].Value=textbox1.Text;

INSERT INTO item (c2) values (@b) 
cmd.Parameters.Add("@b",SqlDbType.Varchar)
cmd.Parameteres["@b"].Value=textbox2.Text;.
.
.
INSERT INTO item (c10) values (@j)
cmd.Parameters.Add("@j",SqlDbType.Varchar)
cmd.Parameteres["@j"].Value=textbox10.Text;

or, please suggest an efficient code.

How to add multiple parameters to cmd in a single statement? is it possible?

sqlchild
  • 8,754
  • 28
  • 105
  • 167

5 Answers5

10

You can use an extension method, like this:

public static class DbCommandExtensions
{
    public static void AddInputParameters<T>(this IDbCommand cmd,
        T parameters) where T : class
    {
        foreach (var prop in parameters.GetType().GetProperties())
        {
            object val = prop.GetValue(parameters, null);
            var p = cmd.CreateParameter();
            p.ParameterName = prop.Name;
            p.Value = val ?? DBNull.Value;
            cmd.Parameters.Add(p);
        }
    }
}

Then call it like this:

cmd.AddInputParameters(new { a = textBox1.Text, b = TextBox2.Text, /* etc */ });

I've used that in a few projects with no problems.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
10

I think you can use Parameters.AddWithValue() method.

cmd.Parameters.AddWithValue("@j",textbox10.Text);
cmd.Parameters.AddWithValue("@k",textbox11.Text);
cmd.Parameters.AddWithValue("@l",textbox12.Text);
Anuraj
  • 18,859
  • 7
  • 53
  • 79
2

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).

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
0

You could use a function like this:

void AddParams(DBCommand cmd,params object[] parameters)
{
    if (parameters != null)
    {
        int index = 0;
        while (index < parameters.Length)
        {
            cmd.Parameters.AddWithValue("@"+(string)parameters[index], parameters[index + 1]);
            index += 2;
        }
    }
}

Not the best one probably, but functional. Call link this:

AddParams(a,"test1",b,3,c,DateTime.Now);

Or you can use an extension like suggested from @Matt Hamilton to add this function in DBCommand class.

Marco
  • 56,740
  • 14
  • 129
  • 152
0

Can also be like this..

cmd.Parameters.Add("@j", SqlDbType.Varchar).Value = textbox10.Text;
  • 1
    Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Aug 31 '23 at 07:23