从c#向存储过程传递参数

本文关键字:参数 存储过程 | 更新日期: 2023-09-27 18:03:28

我是存储过程的新手,我以前通常做的是有一个数据库类并将sql查询传递给选择或更新方法。现在我为我的数据库创建了以下类

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;
        }

从c#向存储过程传递参数

您可以通过添加:

将参数传递给存储过程
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;
}

链接:如何调用具有输出参数的存储过程?