如何将图像的空值传递给SQL表 [英] How to pass null value for image to SQL table

查看:71
本文介绍了如何将图像的空值传递给SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在sql中为图像字段传递Null值或空字节



我尝试了什么:



我的代码在下面

I want to pass Null value or empty bytes for image field in sql

What I have tried:

My Code is below

If Not pic Is Nothing Then
              imageConverter = New ImageConverter()
              imageByte = DirectCast(imageConverter.ConvertTo(pic, GetType(Byte())), Byte())
              objCmd.Parameters.AddWithValue("@EmpPhoto", imageByte)
          Else
              objCmd.Parameters.AddWithValue("@EmpPhoto", DBNull.Value)
          End If



显示错误

操作数类型与图像不兼容


It shows an Error
Operand type is incompatiable with the image

推荐答案

尝试:

Try:
objCmd.Parameters.AddWithValue("@EmpPhoto", SqlDbType.Image).Value = DBNull.Value



有趣。这不起作用......也不会这样:


Interesting. That doesn't work...Nor does this:

try
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con))
            {
            cmd.Parameters.AddWithValue("@P", SqlDbType.Image).Value = DBNull.Value;
            int result = cmd.ExecuteNonQuery();
            }
        }
    }
catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }



但这样做:


But this does:

try
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con))
            {
            SqlParameter ip = new SqlParameter("@P", SqlDbType.Image);
            ip.Value = DBNull.Value;
            cmd.Parameters.Add(ip);
            int result = cmd.ExecuteNonQuery();
            }
        }
    }
catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }





所以......在VB中:



So ... in VB:

Try

    Using con As SqlConnection = New SqlConnection(strConnect)
        con.Open()

        Using cmd As SqlCommand = New SqlCommand("INSERT INTO Table_4 (Photo) VALUES (@P)", con)
            Dim ip As SqlParameter = New SqlParameter("@P", SqlDbType.Image)
            ip.Value = DBNull.Value
            cmd.Parameters.Add(ip)
            Dim result As Integer = cmd.ExecuteNonQuery()
        End Using
    End Using

Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try


这篇关于如何将图像的空值传递给SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆