有关c#的listbox控件的问题。我的设备读取到的id放在listbox里边,想通过id来读取数据库的一行数据。 50
例如我读取到的id是C9D9,CB51,CB53等等,我点击C9D9能出现数据表的第一行数据,点击CB51,能出现数据表的第二行数据,点击CB53能出现数据表的第三行数据...
例如我读取到的id是C9 D9,CB 51,CB 53等等,我点击C9 D9能出现数据表的第一行数据,点击CB 51,能出现数据表的第二行数据,点击CB 53能出现数据表的第三行数据,一次类推。这是我下面的算法(数据库调用写成公共类了直接调用).但是一直都是
private void listBox_Tags_SelectedIndexChanged(object sender, EventArgs e)
{
con.ConDatabase();
string strid = listBox_Tags.SelectedItem.ToString();
string sql1 = "select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID=strid";
SqlCommand cmd = new SqlCommand(sql1, con.conn);
con.closeCon();
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (strid.Substring(36) == "C9 D9 ")
{
while (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
else if (strid.Substring(36) == "CB 51 ")
{
while (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
else if (strid.Substring(36) == "CB 53 ") 展开
private void listBox_Tags_SelectedIndexChanged(object sender, EventArgs e)
{
con.ConDatabase();
string strid = listBox_Tags.SelectedItem.ToString();
string sql1 = "select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID=strid";
SqlCommand cmd = new SqlCommand(sql1, con.conn);
con.closeCon();
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (strid.Substring(36) == "C9 D9 ")
{
while (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
else if (strid.Substring(36) == "CB 51 ")
{
while (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
else if (strid.Substring(36) == "CB 53 ") 展开
1个回答
展开全部
主要错误是那行sql
"select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID=strid";
应该写作
string sql1 =string.format( "select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID='{0}'",strid);
其次, if (strid.Substring(36) == "C9 D9 ")后面的完全不知道你在做什么,如果只是要取出记录,这一行后面全部删除,改为
if (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
完整的就是
using(SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
"select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID=strid";
应该写作
string sql1 =string.format( "select strID,strName,strWorkType,strDept,strTask,strTaskStatus from tb_People where strID='{0}'",strid);
其次, if (strid.Substring(36) == "C9 D9 ")后面的完全不知道你在做什么,如果只是要取出记录,这一行后面全部删除,改为
if (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
完整的就是
using(SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
this.textBox1.Text = dr[0].ToString();
this.textBox2.Text = dr[1].ToString();
this.textBox3.Text = dr[2].ToString();
this.textBox4.Text = dr[3].ToString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询