将checkedlistbox选中的项值传递给存储过程

时间:2013-08-13 17:47:01

标签: c# sql-server stored-procedures checkedlistbox

我有一个CheckedListBox。我希望能够从中选择n个项目,然后将这些项目值(如CheckedListBox中的Text)传递给sproc。我没有连接到DB并调用sproc的问题。我只需要弄清楚如何将所选项目值分配给变量。我应该使用什么数据类型?

我认为我需要弄清楚的是这一部分:

string listingId = checkedListBoxBids.CheckedItems.ToString();

我在下面粘贴了我的代码。前两种方法调用我使用的sprocs;第3种方法根据一些单选按钮选择/单击确认按钮来触发2个sprocs中的一个。

// marks selected listbox item as 'Won'
private void MarkItemAsWon(string itemWon)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();
    //string listingId = checkedListBoxBids.Text.ToString();

    // connection string
    string cnWatermelon = ConfigurationManager.ConnectionStrings["Watermelon.Properties.Settings.watermelonsConnectionString"].ToString();

    SqlConnection watermelonConn = new SqlConnection(cnWatermelon);

    SqlCommand markItemAsWonCommand = new SqlCommand();

    markItemAsWonCommand.CommandType = CommandType.StoredProcedure;
    markItemAsWonCommand.CommandText = "dbo.MarkItemAsWon";

    markItemAsWonCommand.Parameters.Add("@ListingID", SqlDbType.VarChar).Value = listingId;

    SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
    DataTable dt = new DataTable();

    try
    {
        markItemAsWonCommand.Connection = watermelonConn;

        watermelonConn.Open();

        MyDataAdapter.SelectCommand = markItemAsWonCommand;

        MyDataAdapter.Fill(dt);
    }
    catch (Exception exc_PROCESS)
    {
        MessageBox.Show(exc_PROCESS.ToString(), "Error message",
        MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    finally
    {
        watermelonConn.Close();
    }
}


// marks selected listbox item as 'Lost'
private void MarkItemAsLost(string itemLost)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();
    //string listingId = checkedListBoxBids.Text.ToString();

    // connection string
    string cnWatermelon = ConfigurationManager.ConnectionStrings["Watermelon.Properties.Settings.watermelonsConnectionString"].ToString();

    SqlConnection watermelonConn = new SqlConnection(cnWatermelon);

    SqlCommand markItemAsLostCommand = new SqlCommand();

    markItemAsLostCommand.CommandType = CommandType.StoredProcedure;
    markItemAsLostCommand.CommandText = "dbo.MarkItemAsLost";

    markItemAsLostCommand.Parameters.Add("@ListingID", SqlDbType.VarChar).Value = listingId;

    SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
    DataTable dt = new DataTable();

    try
    {
        markItemAsLostCommand.Connection = watermelonConn;

        watermelonConn.Open();

        MyDataAdapter.SelectCommand = markItemAsLostCommand;

        MyDataAdapter.Fill(dt);
    }
    catch (Exception exc_PROCESS)
    {
        MessageBox.Show(exc_PROCESS.ToString(), "Error message",
        MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    finally
    {
        watermelonConn.Close();
    }
}



// reads the option selected in the "Won?" groupbox and marks selected item as either 'Won' or 'Lost', then refreshes the checkedlistbox items
private void buttonWonConfirm_Click(object sender, EventArgs e)
{
    string listingId = checkedListBoxBids.CheckedItems.ToString();

    if (radioButtonWonYes.Checked == true)
    {

        //foreach (object itemChecked in checkedListBoxBids.CheckedItems)
        //{

        //    // show selected items in messagebox
        //    //MessageBox.Show("Item with title: \"" + itemChecked.ToString());

        //    MarkItemAsWon(itemChecked.ToString());
        //}


        MarkItemAsWon(listingId.ToString());

        PopulateBidItems();
        PopulateWonItems();
    }
    else
    {
        MarkItemAsLost(listingId.ToString());

        PopulateBidItems(); 
        PopulateWonItems();
    }
}

1 个答案:

答案 0 :(得分:1)

checkedListBoxBids.CheckedItems将返回已检查项目的集合。根据您的需要,您可以获得每个检查项目或只有下面的项目之一

foreach(object itemChecked in checkedListBoxBids.CheckedItems)
{
     DataRowView item = itemChecked as DataRowView;
     string listingID= item["ListingID"];

}
假设您绑定如下

checkedListBoxBids.DataSource = dt;
checkedListBoxBids.DisplayMember = "ListingName";
checkedListBoxBids.ValueMember = "ListingID";