在Sql2005中,向表中插入数据时遇到uniqueidentifier列,如何插入数据?

Sql2005中,提供了uniqueidentifier 数据类型。说白了,就是个GUID,这种类型开发时倒是很有必要的。

今天程序中遇到了这个问题:表里定义了一个uniqueidentifier 列,Asp.net程序需要向表中插入新的数据。Insert 语句由数据源控件自动生成:INSERT INTO [morning_Department] ([DepartmentId], [name]) VALUES (@DepartmentId, @name),其中DepartmentId列为一个uniqueidentifier 列。

错误提示:
Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query.

解决方法有2种:
1、在数据库中将Insert 操作写成存储过程,uniqueidentifier 列由SQL2005提供的NEWID()函数生成,Asp.net程序这边就不用负责生成这个值了;

2、还是按常规思路,只是在数据源控件InsertQuery属性中稍作修改即可。
①点击InsertQuery属性,打开‘命令和参数编辑器’;
②就上面写的那个Insert 语句来说,在‘参数’列表里选择‘DepartmentId’这个uniqueidentifier 列名称;
③右边有个‘显示高级属性’,点击它。滚动条拖到最下,有个‘Type’属性,默认值是‘Object ’类型,把它改为‘string’。
④Ok,收工。

--------------------- 本文来自 quou2002 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/quou2002/article/details/596509?utm_source=copy 

猜你喜欢

转载自www.cnblogs.com/asdyzh/p/9749452.html