向sqlparameter类添加更多参数

时间:2012-06-15 03:29:19

标签: c# sql

我必须调用一个存储过程,但我有更多的参数是否有任何简单的方法来做到这一点?或者只是将每个参数添加到sqlparameter类?如下所示

 SqlCommand command = new SqlCommand("inserting", con);
 command.CommandType = CommandType.StoredProcedure;
 command.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.NVarChar).Value = TextBox1.Text;

7 个答案:

答案 0 :(得分:7)

请注意Paramters.Add有一个overload,它接受​​一个字符串和一个DbType,因此您不必调用Parameter构造函数。您可以替换当前使用的行来添加新参数:

command.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.NVarChar)).Value = TextBox1.Text;

使用以下更短(但功能相同)的行:

command.Parameters.Add("@Firstname", SqlDbType.NVarChar).Value = TextBox1.Text;

如果您想添加更多参数,只需将它们添加到命令的Parameters属性中,如下所示:

SqlCommand command = new SqlCommand("inserting", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@Firstname", SqlDbType.NVarChar).Value = TextBox1.Text;
command.Parameters.Add("@Lastname", SqlDbType.NVarChar).Value = TextBox2.Text;

除此之外,您是否尝试过使用Parameters.AddWithValue?如果列的数据类型映射到C#中的值类型,则可以使用此方法。您可以找到C#到SQL Server数据类型here的映射。

你会像这样使用它:

// Assume your sproc has a parameter named @Age that is a SqlInt32 type
int age = 5;
// Since age is a C# int (Int32), AddWithValue will automatically set
// the DbType of our new paramter to SqlInt32.
command.Parameters.AddWithValue("@Age", 5);

如果你需要指定SqlDbType,AddWithValue会返回你刚添加的参数,所以它就像添加一个额外的语句来设置DbType property一样简单,尽管此时你最好只需使用原始.Add函数并设置值。

command.Parameters.AddWithValue("@Firstname", TextBox1.Text).DbType = SqlDbType.NVarChar;

答案 1 :(得分:3)

使用SqlParameter类型的Array并将其插入到SqlCommand

SqlCommand Comm = new SqlCommand("Command text", new SqlConnection("Connection String");
SqlParameter[] param = {new SqlParameter("@Name","Value"), 
                        new SqlParameter("@Name","Value"),
                        ........
                        };
Comm.Parameters.AddRange(param);

答案 2 :(得分:1)

只需多次调用command.Parameters.Add方法:

 SqlCommand command = new SqlCommand("inserting", con);
 command.CommandType = CommandType.StoredProcedure;

 command.Parameters.Add("@Firstname", SqlDbType.NVarChar, 100).Value = TextBox1.Text;
 command.Parameters.Add("@Lastname", SqlDbType.NVarChar, 100).Value = TextBox2.Text;
 command.Parameters.Add("@City", SqlDbType.NVarChar, 100).Value = TextBox3.Text;
 command.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(TextBox4.Text);
 ....... and so on .....

答案 3 :(得分:0)

您可以使用dapper-dot-net

示例代码:

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

插入示例:

connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  ).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"

答案 4 :(得分:0)

不推荐使用command.Parameters.Add。而是使用command.Parameters.AddWithValue。 为此,您可以为每个参数多次调用它。

答案 5 :(得分:0)

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 6 :(得分:0)

您可以像这样使用

return new SqlParameter[]
            {
                new SqlParameter("@Firstname", SqlDbType.VarChar)
                {
                    Value = Firstname.Text
                },
                new SqlParameter("@Lastname", SqlDbType.VarChar)
                {
                    Value = Lastname.Text
                },
            };