Agoni_Soul
2017-03-04 04:40
采纳率: 0%
浏览 2.1k

C#中如何用comBox控件被点击后所对应的项查找数据库后输出相应的值??

控件代码如下

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        sqlcon = new SqlConnection(strcon);
        sqlcon.Open();
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.Connection = sqlcon;
        string name = "";
        name = comboBox1.Text.ToString();
        Console.WriteLine(comboBox1.Text.ToString());
        sqlcmd.CommandText = "select Money from nini where Name = " + name.ToString(). Trim();
        sqlcmd.CommandType = CommandType.Text;
        SqlDataReader reader = sqlcmd.ExecuteReader();
        while (reader.Read())
        {
            listView1.Items.Add(reader[0].ToString());
            textBox1.Text = reader[0].ToString();
        }
    }

            ![图片说明](https://img-ask.csdn.net/upload/201703/04/1488601975_121709.png)

            但是报错了,如何才能解决
            ![图片说明](https://img-ask.csdn.net/upload/201703/04/1488602066_109729.png)


            数据库数据如下
            ![图片说明](https://img-ask.csdn.net/upload/201703/04/1488602135_605182.png)
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • threenewbee 2017-03-04 15:03
    打赏 评论
  • _孤星 2017-03-06 02:26

    你的那个列名值在SQL语句里没有用单引号括住

    打赏 评论
  • JackyRao 2017-04-10 06:31

    sqlcmd.CommandText = "select Money from nini where Name = '" + name.ToString(). Trim()+"'";

    打赏 评论

相关推荐 更多相似问题