从C#传递参数到存储过程

时间:2014-05-15 15:53:56

标签: c# sql-server stored-procedures

我是Stored Procedures的新手,我之前通常做的是拥有一个数据库类并将sql Query传递给select或update方法。现在我有以下课程用于我的数据库

class DatabaseService
    {

        static SqlConnection connection;

        public void getConnection()
        {

            try
            {
                connection = new SqlConnection("Data Source=PC-DILUKSHAN\\SQLEXPRESS;Initial Catalog=SPTESTDB;User ID=user;Password=1234");
                connection.Open();

            }
            catch (Exception e)
            {

                throw e;
            }

        }
        public DataTable executeSelectQuery(String sql)
        {
            getConnection();

            try
            {
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = CommandType.StoredProcedure;
                // Create a DataAdapter to run the command and fill the DataTable
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;

            }
            catch (Exception e)
            {

                throw e;
            }
            finally
            {

                connection.Close();
            }

        }

        public void executeUpdateQuery(String sql)
        {
            getConnection();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {

                throw e;
            }
            finally
            {
                connection.Close();

            }

        }
    }

我的数据库类的这个方法是为了所有选择和更新事务而创建的常用,但是将这些参数添加到类本身确实使它特定于该事务,对吧?我可以在表单加载事件中添加参数吗?

现在在我的WinForm Load事件中,我通过存储过程填充数据网格,但我需要将@cusid传递给它。代码如下所示我收到错误说"程序或功能' spCustomerParam'期望参数' @ cusid',这是未提供的。"

 private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = Program.db.executeSelectQuery("spCustomerParam");
            dataGridView1.DataSource = dt;

        }

1 个答案:

答案 0 :(得分:1)

您可以通过添加:

将param传递给存储过程
cmd.Parameters.AddWithValue(“@Name”, value);

所以你的代码可以通过添加parameters参数和默认值来扩展(以保持向后的能力):

public DataTable executeSelectQuery(String sql, Dictionary<String,String> parameters = null)
{
    getConnection();

    try
    {
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.StoredProcedure;


        if (parameters != null)
        {
            foreach (var param in parameters)
            {
                cmd.Parameters.AddWithValue("@"+param.Key, param.Value);
            }            
        }
        // Create a DataAdapter to run the command and fill the DataTable
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.Fill(dt);

        return dt;    
    }
    catch (Exception e)
    {    
        throw e;
    }
    finally
    {    
        connection.Close();
    }    
}

并致电:

private void Form1_Load(object sender, EventArgs e)
{
    var parameters = new Dictionary<String, String>() {{"param1", "paramValue1"}, {"param2", "paramValue2"}};

    DataTable dt = Program.db.executeSelectQuery("spCustomerParam", parameters);
    dataGridView1.DataSource = dt;

}

链接: How to call a stored procedure with output parameters?