为什么我不能将 DBNull.Value 插入到 sql server 2005 中的可为空图像字段中?
为什么我无法将 DBNull.Value
插入 sql server 2005 中可为空的 image
字段?
我尝试了这段代码:
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=PrescriptionTrackingSystem;Integrated Security=True");
conn.Open();
SqlTransaction transaction = conn.BeginTransaction();
SqlCommand command = new SqlCommand(@"INSERT INTO Customer(
ID
,Name
,TelephoneNumber
,DateOfBirth,
InsuranceProvider,
PolicyNumber,Photo)
VALUES(
@ID
,@Name
,@TelephoneNumber
,@DateOfBirth,
@InsuranceProvider,
@PolicyNumber,
@Photo)", conn);
command.Transaction = transaction;
command.Parameters.AddWithValue("@ID", 1000);
command.Parameters.AddWithValue("@Name", item.Name);
command.Parameters.AddWithValue("@TelephoneNumber", item.TelephoneNumber);
if (item.DateOfBirth != null)
{
command.Parameters.AddWithValue("@DateOfBirth", item.DateOfBirth);
}
else
{
command.Parameters.AddWithValue("@DateOfBirth", DBNull.Value);
}
command.Parameters.AddWithValue("@InsuranceProvider", item.InsuranceProvider);
command.Parameters.AddWithValue("@PolicyNumber", item.PolicyNumber);
if (item.Photo != null)
{
command.Parameters.AddWithValue("@Photo", item.Photo);
}
else
{
command.Parameters.AddWithValue("@Photo", DBNull.Value);
}
int count = command.ExecuteNonQuery();
transaction.Commit();
conn.Close();
项目的类型为Customer
。
public class Customer
{
public string Name { get; set; }
public DateTime? DateOfBirth { get; set; }
public string TelephoneNumber { get; set; }
public string InsuranceProvider { get; set; }
public int? PolicyNumber { get; set; }
public byte [] Photo { get; set; }
}
插入时出现此异常:
Operand type clash: nvarchar is incompatible with image
Why DBNull.Value
has had issues only with image
?为什么不使用日期时间
?
Why can't I insert DBNull.Value
into a nullable image
-field in sql server 2005?
I tried this code:
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=PrescriptionTrackingSystem;Integrated Security=True");
conn.Open();
SqlTransaction transaction = conn.BeginTransaction();
SqlCommand command = new SqlCommand(@"INSERT INTO Customer(
ID
,Name
,TelephoneNumber
,DateOfBirth,
InsuranceProvider,
PolicyNumber,Photo)
VALUES(
@ID
,@Name
,@TelephoneNumber
,@DateOfBirth,
@InsuranceProvider,
@PolicyNumber,
@Photo)", conn);
command.Transaction = transaction;
command.Parameters.AddWithValue("@ID", 1000);
command.Parameters.AddWithValue("@Name", item.Name);
command.Parameters.AddWithValue("@TelephoneNumber", item.TelephoneNumber);
if (item.DateOfBirth != null)
{
command.Parameters.AddWithValue("@DateOfBirth", item.DateOfBirth);
}
else
{
command.Parameters.AddWithValue("@DateOfBirth", DBNull.Value);
}
command.Parameters.AddWithValue("@InsuranceProvider", item.InsuranceProvider);
command.Parameters.AddWithValue("@PolicyNumber", item.PolicyNumber);
if (item.Photo != null)
{
command.Parameters.AddWithValue("@Photo", item.Photo);
}
else
{
command.Parameters.AddWithValue("@Photo", DBNull.Value);
}
int count = command.ExecuteNonQuery();
transaction.Commit();
conn.Close();
item is of type Customer
.
public class Customer
{
public string Name { get; set; }
public DateTime? DateOfBirth { get; set; }
public string TelephoneNumber { get; set; }
public string InsuranceProvider { get; set; }
public int? PolicyNumber { get; set; }
public byte [] Photo { get; set; }
}
When inserting I get this exception:
Operand type clash: nvarchar is incompatible with image
Why DBNull.Value
has got problems only with image
? Why not with datetime
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以试试这个:
You could try this:
我认为这可能与您没有显式定义 SqlParameter.SqlDbType 并且认为它将假设 NVARCHAR 有关。尝试添加新的 SqlParameter 并将 SqlDbType 显式设置为 SqlDbType.Image,而不是使用 AddWithValue。
这是 MSDN 参考,其中指出默认为 NVARCHAR。
I think this could be to do with the fact that you are not explicitly defining the SqlParameter.SqlDbType and think it will be assuming NVARCHAR. Instead of using AddWithValue, trying adding a new SqlParameter and setting SqlDbType explicitly to SqlDbType.Image.
Here's the MSDN ref which states the default is NVARCHAR.
您需要显式指定参数的类型为 SqlDbType.Image
You need to explicitly specify the parameter's type as SqlDbType.Image
我这样解决了这个问题:
I solved this like this: