不允许从数据类型sql_variant隐式转换为uniqueidentifier.使用CONVERT函数运行此查询 [英] Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query

查看:55
本文介绍了不允许从数据类型sql_variant隐式转换为uniqueidentifier.使用CONVERT函数运行此查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Microsoft Visual Web Developer 2010 Express创建一个网站,并且使用了"createUserWizard" 的工具箱.然后,将我的"CustomerInfo" 表的 userId 放入"data type = uniqueidentifier" ,因为我需要将其链接到用户名在aspnet_表中.

I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard". Then I put the userId for my "CustomerInfo" table to the "data type=uniqueidentifier" because I need to link it to the user name in the aspnet_ table.

后来,我需要将我的"Order" 表链接到"CustomerInfo" 表,因此我将我的 orderId数据类型= uniqueidentifier .然后,我计划将订单详细信息插入到"Order" 表中,但是我遇到了以下问题:

Later on, I need to link my "Order" table to the "CustomerInfo" table so I put my orderId data type=uniqueidentifier. Then, I plan insert my order details to the "Order" table but I have the problem of:

从数据类型隐式转换sql_variant到uniqueidentifier不是允许的.使用转换功能运行此查询".

"Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query".

我搜索并找到一些答案,例如设置为"Empty"的参数的数据类型或将其删除.但是我有这个错误

I search and find some answer like datatype of the parameter set to "Empty"or delete it. but then I have this error

从字符串转换为uniqueidentifier时转换失败."

" Conversion failed when converting from character string to uniqueidentifier."

这是我的SQL

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                    DeleteCommand="DELETE FROM [Order] WHERE [orderId] = @orderId" 
                                    InsertCommand="INSERT INTO [Order] ([orderId], [userId], [Services], [PickUpDate], [PickUpTime], [SpecialDate], [SpecialTime]) VALUES (@orderId, @userId, @Services, @PickUpDate, @PickUpTime, @SpecialDate, @SpecialTime)" 
                                    SelectCommand="SELECT * FROM [Order]" 

                                    UpdateCommand="UPDATE [Order] SET [userId] = @userId, [Services] = @Services, [PickUpDate] = @PickUpDate, [PickUpTime] = @PickUpTime, [SpecialDate] = @SpecialDate, [SpecialTime] = @SpecialTime WHERE [orderId] = @orderId">
                                    <DeleteParameters>
                                        <asp:Parameter Name="orderId" Type="Object" />
                                    </DeleteParameters>
                                    <InsertParameters>
                                    <asp:Parameter Name="orderId" Type="Object" />
                                    <asp:Parameter Name="userId" Type="Object" />
                                        <asp:Parameter Name="Services" Type="String" />
                                        <asp:Parameter Name="PickUpDate" Type="String" />
                                        <asp:Parameter Name="PickUpTime" Type="String" />
                                        <asp:Parameter Name="SpecialDate" Type="String" />
                                        <asp:Parameter Name="SpecialTime" Type="String" />
                                    </InsertParameters>
                                    <UpdateParameters>
                                        <asp:Parameter Name="userId" Type="Object" />
                                        <asp:Parameter Name="Services" Type="String" />
                                        <asp:Parameter Name="PickUpDate" Type="String" />
                                        <asp:Parameter Name="PickUpTime" Type="String" />
                                        <asp:Parameter Name="SpecialDate" Type="String" />
                                        <asp:Parameter Name="SpecialTime" Type="String" />
                                        <asp:Parameter Name="orderId" Type="Object" />
                                    </UpdateParameters>
                                </asp:SqlDataSource>

现在我想到了,也许我在vb中的代码有问题.我将其放在此处,请告诉我如何执行将数据插入数据库的正确方法.

Now that I think of it, maybe there is a problem with my code in vb. I will put it here, please tell me how to do the correct way of inserting data to the database.

Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
    SqlDataSource1.InsertParameters(0).DefaultValue = Now
    SqlDataSource1.Insert()
    Response.Redirect("~/Customer/AfterOrder.aspx")
End Sub

推荐答案

如果要从代码中插入GUID,则应将参数类型设置为GUID

If you want to insert GUID from your code, you should make parameter type GUID

<asp:Parameter Name="ParamName" DbType="Guid" />

您应该将GUID值传递为(c#):

And you should pass GUID value as (c#):

SqlDataSource1.InsertParameters["ParamName"].DefaultValue = System.Guid.NewGuid().ToString();

如果要传递字符串值,则必须在插入查询中使用 CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE'),如下所示:

If you want to pass a string value, you have to use CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE') in the insert query like this:

InsertCommand="INSERT INTO [Order] ([UserId], [etc]) 
VALUES (CONVERT(UNIQUEIDENTIFIER,@UserId), @etc)" 

这篇关于不允许从数据类型sql_variant隐式转换为uniqueidentifier.使用CONVERT函数运行此查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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