2

这是我正在使用的代码..我想将下拉选择的项目作为输入传递给存储过程并执行..我怎么能做到这一点?

public class DaysForEvents
{
    public DataSet GetDaysForEvents(//This should be my input to stored procedure (i.e)  dropdownselected value)
    {
        SqlConnection con = new SqlConnection(CommonSettings.GetEventsConnectionString());

        try
        {
            SqlCommand sqlcmd = new SqlCommand("proc_fetch_event_days", con);
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
           //DateFilter = 0;
            //StatusFilter = 0;

            sqlcmd.CommandType = CommandType.StoredProcedure;

            sqlcmd.Parameters.AddWithValue("@event_id", Event_id);
            // sqlcmd.Parameters["@UserID"].Value = UserID;


            con.Open();
            sqlda.Fill(ds);
            con.Close();

            return ds;


        }
4

4 回答 4

1

据我了解,你想要这个。

//get dropdown selected value and store in a variable 
var eventId = dropdown.SelectedValue; 

//pass this variable in the GetDaysForEvents method to get DataSet.
var dataSet = GetDaysForEvents(eventId); 
于 2012-08-28T09:09:58.803 回答
0

调用你的函数

string eventId = ddlEvents.SelectedValue;
GetDaysForEvents(eventId);
于 2012-08-28T09:11:19.080 回答
0
sqlcmd.Parameters.AddWithValue("@event_id", yourDropDown.SelectedValue);
于 2012-08-28T09:09:00.797 回答
0

-用户using声明关闭您的连接并处理您未使用的资源。

-DataAdapter自己打开连接,所以不需要Open()

public DataSet GetDaysForEvents(string eventId)
{
    var ds = new DataSet();
    using(var connection = new SqlConnection("ConnectionString"))
    {
        using(var cmd = new SqlCommand("storedProcedureName", connection){ CommandType = CommandType.StoredProcedure })
        {
            cmd.Parameters.AddWithValue("@EventId", eventId);
            using(var adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(ds);
            }
        }
    }
    return ds;
}

还有别的地方

var events = yourClassInstance.GetDaysForEvents(dropDown.SelectedValue);
于 2012-08-28T09:16:34.797 回答