【发布时间】:2018-03-07 12:58:05
【问题描述】:
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["DeathWish"].ToString();
con.Open();
string query = string.Format("Select [ID],Decision from Data where [ID]='{0}' order by Decision", textBox1.Text);
SqlCommand cmd = new SqlCommand(query,con);
SqlDataReader dr = cmd.ExecuteReader();
string[] s = new string[] { };
while (dr.Read())
{
s = dr["Decision"].ToString().Split(',');
}
int length = s.Length;
for (int i = 0; i < length - 1; i++)
{
string fetr = s[i];
for (int j = 0; j <= checkedListBox1.Items.Count - 1; j++)
{
if (checkedListBox1.Items[j].ToString() == s[i])
{
checkedListBox1.SetItemChecked(j, true);
break;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.ToString());
}
}
string query = string.Format("Select [ID],Decision from Data where [ID]='{0}' order by Decision", textBox1.Text); 是有错误的行。
已编辑第二张图片* 我想使用复选框列表检索数据库中的特定值
这是图片:
error msg i wanted to retrieve the data on the database and show the specific value
【问题讨论】:
-
那么,在您尝试设置连接字符串之前,
con是否已经打开?它是一个字段而不是方法变量这一事实表明其他方法可以访问它,因此请检查您是否忘记在某个地方关闭它(见鬼,您甚至没有在button2_Click方法中关闭它)。跨度> -
或者在更改连接字符串或再次打开之前检查连接是否打开。
-
另外你应该阅读msdn.microsoft.com/en-us/library/…这将帮助你防止Sql Injections。
-
"string query = string.Format...is the line with error" - 实际上我怀疑它是 它之前的两行
标签: c#