使用带的DBNull.Value的SqlParameter不知道的SqlDbType? [英] Using DBNull.Value with SqlParameter without knowing sqlDbType?

查看:503
本文介绍了使用带的DBNull.Value的SqlParameter不知道的SqlDbType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个的SqlParameter 的空值传递到各种列可以为空表。问题是,的SqlParameter看起来像它默认为nvarchar如果没有的SqlDbType为present。这presents一个问题,如果实际的数据库类型是VARBINARY;我得到一个异常说:

I'm using a SqlParameter to pass in null values to a table for various columns that are nullable. The problem is that SqlParameter looks like it defaults to nvarchar if no sqlDbType is present. This presents a problem if the actual db type is varbinary; I get an exception saying

从数据类型为nvarchar隐式转换为VARBINARY(最大)是不允许的。使用CONVERT函数来运行此查询。

Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

当我创建的SqlParameter,我所知道的是参数的名称和对象。如果对象是空的,很明显的SqlParameter无法推断正确的类型来使用,那么,有没有办法使用的SqlParameter空值,而无需创建SQL参数时知道的SqlDbType?

When I create the SqlParameter, all I know is the parameter's name, and object. If the object is null, SqlParameter obviously can't infer the right type to use, so is there a way to use SqlParameter with null values, without having to know the sqlDbType when creating the sql parameter?

从本质上传递的DBNull到数据库而不指定的类型,并让数据库处理呢?

Essentially pass the DBNull to the database without specifying the type, and let the database handle it?

推荐答案

老的文章,但可能会有所帮助别人。

Old post but might be helpful someone else.

Convert.DBNull

像这样

command.Parameters.AddWithValue("@param", Convert.DBNull);

这篇关于使用带的DBNull.Value的SqlParameter不知道的SqlDbType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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