Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我得到的未处理SQLException来自哪里?_C#_Image_Exception_Sql Server Express - Fatal编程技术网

C# 我得到的未处理SQLException来自哪里?

C# 我得到的未处理SQLException来自哪里?,c#,image,exception,sql-server-express,C#,Image,Exception,Sql Server Express,我有这个密码 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection cnn; string connectionString; connectionString = "server=.\\sqlexpress;database=Blue;trusted_connection=true"; cnn =

我有这个密码

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cnn;
        string connectionString;
        connectionString = "server=.\\sqlexpress;database=Blue;trusted_connection=true";
        cnn = new SqlConnection(connectionString);

        MemoryStream stream = new MemoryStream();
        cnn.Open();
        SqlCommand command = new SqlCommand("select Image from ImageParts where ImageName=" + listBox1.SelectedIndex, cnn);
        byte[] image = (byte[])command.ExecuteScalar();
        stream.Write(image, 0, image.Length);
        cnn.Close();
        Bitmap bitmap = new Bitmap(stream);
        pictureBox1.Image = bitmap;
    }
我已将
ImageName
存储在
listBox1中。Items

然后,错误来了

将varchar值“c1.jpg”转换为数据时,转换失败 输入int


有什么问题吗?由于我是C#新手,我对这些错误并不熟悉。

看起来数据库中的
ImageName
字段的数据类型是
int
,您正在向其传递
字符串(
varchar


验证并更正数据库中
ImageName
的数据类型。

我假设数据库中的ImageName列是字符串类型,而不是int类型。您正在将索引而不是图像的名称传递给SqlCommand。如果是这种情况,不要忘记在字符串周围使用单引号

string nameImg = listBox1.SelectedItem.ToString();
SqlCommand command = new SqlCommand("select Image from ImageParts where ImageName = '" + nameImg + "'");

在这里,我假设您的列表框中填充了图像的字符串名称,以及Steve所说的内容,您还应该在SQL命令上用try-catch块分隔符包装代码,并查看发送到服务器的值。在即时窗口中或通过手表获取命令.CommandText.ToString(),复制该值并尝试在SSMS中运行它,分析它,查看发生了什么,并相应地调整字符串。这样,从现在开始,您可以看到它失败的原因,并在程序失败时相应地处理错误。

我同意Shark的观点,这里的问题是数据类型不匹配