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

C# 从C向存储过程传递参数#,c#,sql-server,stored-procedures,C#,Sql Server,Stored Procedures,我不熟悉存储过程,以前我通常使用的是数据库类,并将sql查询传递给select或update方法。现在我的数据库有以下类 class DatabaseService { static SqlConnection connection; public void getConnection() { try { connection = new SqlConne

我不熟悉存储过程,以前我通常使用的是数据库类,并将sql查询传递给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();

            }

        }
    }
我的数据库类的这些方法是为所有select和update事务的通用性而创建的,但将这些参数添加到类本身确实使其特定于该事务,对吗?我可以在表单加载事件中添加参数吗

现在,在我的WinForm加载事件中,我通过一个存储过程用数据填充网格,但我需要将@cusid传递给它。代码如下所示,我在其中得到一个错误:“过程或函数'spCustomerParam'需要参数'@cusid',但未提供该参数。”


您可以通过添加以下内容将param传递给存储过程:

cmd.Parameters.AddWithValue(“@Name”, value);
因此,可以通过添加带有默认值的
参数
参数来扩展代码(以保持向后功能):

公共数据表executeSelectQuery(字符串sql,字典参数=null) { getConnection(); 尝试 { SqlCommand cmd=新的SqlCommand(sql,连接); cmd.CommandType=CommandType.storedProcess; if(参数!=null) { foreach(参数中的var参数) { cmd.Parameters.AddWithValue(“@”+param.Key,param.Value); } } //创建DataAdapter以运行命令并填充DataTable SqlDataAdapter da=新的SqlDataAdapter(); da.SelectCommand=cmd; DataTable dt=新的DataTable(); da.填充(dt); 返回dt; } 捕获(例外e) { 投掷e; } 最后 { 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;

}
private void Form1\u加载(对象发送方,事件参数e)
{
var parameters=newdictionary(){{“param1”,“paramValue1”},{“param2”,“paramValue2”};
DataTable dt=Program.db.executeSelectQuery(“SPCusterParam”,参数);
dataGridView1.DataSource=dt;
}
链接:

您只需搜索错误,请尝试此处:您需要在代码中创建一个参数,将所需的值传递给SProct。我的数据库类的此方法是为所有select和update事务而创建的,但将这些参数添加到类本身会使其特定于该事务,对吗?我可以在表单加载事件中添加参数吗?您可以在表单加载事件中添加参数,请查看我的更改。现在,这很有意义,我将尝试一下!非常感谢。太好了!这个很好用!非常感谢!
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;

}