Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向以下存储过程调用添加参数?_C#_Sql Server_Stored Procedures - Fatal编程技术网

C# 如何向以下存储过程调用添加参数?

C# 如何向以下存储过程调用添加参数?,c#,sql-server,stored-procedures,C#,Sql Server,Stored Procedures,如何向以下存储过程调用添加参数 using (var conn = new SqlConnection(connectionString)) using (var command = new SqlCommand("ProcedureName", conn) { CommandType = CommandType.StoredProcedure }) { conn.Open(); command.ExecuteNonQuery(

如何向以下存储过程调用添加参数

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) { 
                           CommandType = CommandType.StoredProcedure }) {
   conn.Open();
   command.ExecuteNonQuery();
   conn.Close();
}
像这样:

// this would work for a varchar or nvarchar parameter
command.Parameters.AddWithValue("@yourParameter", "someValue");
// this would work for an integer parameter
command.Parameters.AddWithValue("@someInt", 1234);

显然,在尝试调用
命令.ExecuteNonQuery()之前,您需要任何代码向
参数
集合添加参数

我想你需要更具体一些


使用
命令.Parameters.AddWithValue有什么问题?

这可能是一个解决方案:
此参数应该是存储过程中参数的确切名称(“yourParameter”)

使用(var conn=new SqlConnection(connectionString))
{
var command=new-SqlCommand(“ProcedureName”,conn){CommandType=CommandType.StoredProcedure};
command.Parameters.AddWithValue(“@yourParameter”,“someValue”);
连接打开();
command.ExecuteNonQuery();
连接关闭();
}

您可以使用


您可以使用command.Parameters.AddWithValue(“@number”,TextBox1.Text)


快乐编码

有关更多详细信息,请访问此链接:

下面的代码是从上面发布的链接复制的

static void GetSalesByCategory(string connectionString,string categoryName)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;

        command.CommandText = "SalesByCategory";       
        command.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@CategoryName";
        parameter.SqlDbType = SqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = categoryName;

        // Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);

        // Open the connection and execute the reader.
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}

您研究过command.Parameters了吗?因为您有一个使用
块的
,所以不需要调用
conn.Close()显式。帮你自己一个忙,不要直接使用ado.net,如果你不能使用C#4功能,有很多选择,你可以使用简洁、海量、Petapoco等
command.Parameters.Add("@SomeParmeter", SqlDbType.Int); //SqlDbType is enum
static void GetSalesByCategory(string connectionString,string categoryName)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;

        command.CommandText = "SalesByCategory";       
        command.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@CategoryName";
        parameter.SqlDbType = SqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = categoryName;

        // Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);

        // Open the connection and execute the reader.
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}