简体   繁体   English

如何检查我是否有来自 SQL 服务器数据库的图像或不使用 C#

[英]How can I check if I have an image from my SQL Server database or not using C#

I have a Winforms application which stores products from clients.我有一个 Winforms 应用程序,它存储来自客户的产品。 Those products can have an image to describe them.这些产品可以有一个图像来描述它们。

My problem is that when I want to change my product information I'm getting the following exception:我的问题是,当我想更改我的产品信息时,出现以下异常:

System.InvalidCastException: 'Impossible to cast an object of type "System.DBNull" in type "System.Byte[]" System.InvalidCastException:'不可能在“System.Byte []”类型中转换“System.DBNull”类型的 object

I know this exception occurs because I don't have an image in my database, but I want to know how to check if the image exists.我知道发生此异常是因为我的数据库中没有图像,但我想知道如何检查图像是否存在。

My code:我的代码:

private void chargeImage()
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);

    SqlCommand com1 = new SqlCommand("SELECT * FROM [dbo].[Product] WHERE RFID = '" + dataview.SelectedRows[0].Cells[0].Value.ToString() + "'", con);   // Charge image

    con.Open();

    SqlDataReader dr = com1.ExecuteReader();

    if (dr.Read())
    { 
        byte[] picArr = (byte[])dr["image"];
        ms = new MemoryStream(picArr);
        ms.Seek(0, SeekOrigin.Begin);
        pictureBox1.Image = Image.FromStream(ms);
    }

    con.Close();
}

You can check is it is not DBNull.您可以检查它是否不是 DBNull。 Before

byte[] picArr = (byte[])dr["image"];

you can check你可以检查

if (!Convert.IsDBNull(dr["image"]))

you can check if they contain null value:您可以检查它们是否包含 null 值:

byte[] picArr = dr.IsDBNull("image")? null: (byte[])dr["image"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在连接到数据库之前,如何检查MySQL服务器是否从C#MOBILE处于活动状态 - How can i check if MySQL server is alive from C# MOBILE before i connect to my database 我的 SQL 服务器上有一个检查约束,如何在 c# 中设置检查约束的验证? - I have a check constraint on my SQL server, how do i set a validation for the Check Constraint in c#? 我想将图像从C#表单应用程序保存到SQL Server数据库 - I want to save an image from my C# form application to SQL Server Database 如何使用C#在SQL Server数据库中创建视图? - How can I create a view in a SQL Server database with C#? 使用c#如何以编程方式检查报表服务器上是否存在文件夹 - Using c# how can I programmatically check if a folder exists on my reporting server 如何确保C#应用程序只能从SQL Server数据库读取? - How can I ensure that my C# application can only read from the SQL Server databases? 我可以从一个 SQL 数据库中获取数据,然后使用 MVC C# 将该数据发布到不同服务器上的另一个 SQL 数据库中吗? - Can I get data from one SQL database and then post that data on another SQL database on a diferent server using MVC C# 如何只使用C#获取SQL Server数据库中的表列表以及主键? - How can I get a list of tables in a SQL Server database along with the primary key using ONLY C#? 如何使用C#在SQL Server数据库中存储哈希? - How can I store a hash in a SQL Server database using C#? 如何使用 C# 检查我的 IIS 是否还活着? - How can I check if my IIS is alive using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM