質問

I'm new to Stored Procedures, What I normally did before was to have a database class and passing sql Query to the select or update method. Now I have following class for My database

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();

            }

        }
    }

This methods of my database class was created for common use, to all select and update transactions, but adding those params to class itself does make it specific for that transaction, right? can i add the parameter inside the form load event?

Now in my WinForm Load event I'm populating a grid with data through a Stored Procedure but i need to pass @cusid to it. Code looks like below where i get an error saying "Procedure or function 'spCustomerParam' expects parameter '@cusid', which was not supplied."

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

        }
役に立ちましたか?

解決

You can pass param to stored procedure by adding:

cmd.Parameters.AddWithValue(“@Name”, value);

So your code could be extended by adding parameters argument with default value (to keep backward capability):

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

And call:

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;

}

Links: How to call a stored procedure with output parameters?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top