a1326490804
Agoni_Soul
采纳率0%
2017-03-04 04:40 阅读 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条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-03-04 15:03
    点赞 1 评论 复制链接分享
  • u012494939 _孤星 2017-03-06 02:26

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

    点赞 1 评论 复制链接分享
  • JackyRao JackyRao 2017-04-10 06:31

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

    点赞 评论 复制链接分享

相关推荐