2

我必须调用一个存储过程,但我有更多的参数,有什么简单的方法可以做到这一点吗?或者只是将每个参数添加到 sqlparameter 类?像下面

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

7 回答 7

8

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

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

使用以下较短(但功能等效)的行:

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

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

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 数据类型的映射。

你会像这样使用它:

// 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属性一样简单,虽然此时你最好使用原始的.Add功能和设置值。

command.Parameters.AddWithValue("@Firstname", TextBox1.Text).DbType = SqlDbType.NVarChar;
于 2012-06-15T03:34:00.587 回答
5

使用 SqlParameter 类型的数组并将其插入 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);
于 2012-06-15T05:15:28.667 回答
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 .....
于 2012-06-15T05:21:26.843 回答
1

你可以用喜欢

return new SqlParameter[]
            {
                new SqlParameter("@Firstname", SqlDbType.VarChar)
                {
                    Value = Firstname.Text
                },
                new SqlParameter("@Lastname", SqlDbType.VarChar)
                {
                    Value = Lastname.Text
                },
            };
于 2020-07-10T21:42:42.593 回答
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"
于 2012-06-15T03:43:12.267 回答
0

command.Parameters.Add弃用。而是使用command.Parameters.AddWithValue. 为此,您会为每个参数多次调用它。

于 2016-06-10T07:22:35.070 回答
0
  // Mention size of the nvarchar column   , here i give 500 , you can use its length for @Firstname as you mention in database according to your database 

   SqlCommand command = new SqlCommand("inserting", con);
 command.CommandType = CommandType.StoredProcedure;
 command.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.NVarChar,500).Value = TextBox1.Text;
于 2016-09-09T10:17:34.557 回答