使用数据库数据标记一些在Windows窗体中检查的checklistbox项目 [英] using database data to mark some checklistbox items as checked in windows form
本文介绍了使用数据库数据标记一些在Windows窗体中检查的checklistbox项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码将已在checkedlistbox中检查过的数据
保存到sql db中。现在我想选择保存的数据并在核对表框中进行检查。请帮助
me out
Am using the following codes to save data which have been checked in a checkedlistbox
into sql db. Now i want select the saved data and checked them in the checklistbox. Please help
me out
public void saveMinistry()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike";
string StrQuery;
foreach (var checkedItem in this.cblMinistries.CheckedItems)
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
StrQuery = @"INSERT INTO tblMembershipministry VALUES ('" + this.txtMembershipid2.Text + "', '" + checkedItem + "')";
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
//MessageBox.Show("Record was successfully Added");
}
我正在尝试使用此代码但无法正常工作。帮帮我
am trying to use this code but not working. help me out
public void searchMinistry10()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike";
string StrQuery;
foreach (var checkedItem in this.cblMinistries.CheckedItems)
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
StrQuery = @"SELECT * FROM tblMembershipministry WHERE FormattedMembershipid = '" + this.txtMembershipid2.Text + "'";
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
//MessageBox.Show("Record was successfully Added");
}
推荐答案
您需要编写一个SELECT语句来从数据库中提取数据。然后循环遍历数据,可能使用DataReader,并设置相应的复选框。
You need to write a SELECT statement to pull the data from the database. Then you loop through your data, probably using a DataReader, and set the appropriate checkboxes.
这篇关于使用数据库数据标记一些在Windows窗体中检查的checklistbox项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文