我有一个窗口表单,可以将表(tblMinistries)
(例如数据 A B C D E
)中的数据加载到核对表框(cblMinistries)
中。还有另一个表(tblMembershipministry)
(例如数据 A B C
),其中还包含数据。现在我想要在单击按钮时运行SQL语句,遍历记录然后在两个表中找到匹配的复选框列表项并在核对表框中检查它们。
我尝试过类似的东西,但没有工作
public void Newsearch() {
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike";
conn.Open();
SqlCommand cmd = new SqlCommand();
string sqlQuery = "SELECT Ministryname FROM tblMembershipministry WHERE FormattedMembershipid = '" + this.txtMembershipid.Text + "'";
// System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
System.Data.SqlClient.SqlDataReader dr = null;
dr = cmd.ExecuteReader();
using(SqlDataAdapter people = new SqlDataAdapter(sqlQuery, conn)) {
DataTable people1 = new DataTable();
people.Fill(people1);
foreach(DataRow row in people1.Rows) {
cblMinistries.SetItemCheckState(checked);
}
}
}