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个回答

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐