不允许从数据类型sql_variant到uniqueidentifier的隐式转换。使用CONVERT函数运行此查询

时间:2021-12-10 16:25:26

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.

我正在使用Microsoft Visual Web Developer 2010 Express创建一个网站,我使用了“createUserWizard”工具箱。然后我将userId用于我的“CustomerInfo”表到“data type = uniqueidentifier”,因为我需要将它链接到aspnet_表中的用户名。

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:

稍后,我需要将“Order”表链接到“CustomerInfo”表,因此我将orderId数据类型= uniqueidentifier。然后,我计划将我的订单详细信息插入“订单”表,但我遇到的问题是:

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

“不允许从数据类型sql_variant到uniqueidentifier的隐式转换。使用CONVERT函数运行此查询”。

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

我搜索并找到一些答案,如参数设置为“空”的数据类型或删除它。但后来我有这个错误

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

“从字符串转换为uniqueidentifier时转换失败。”

This is my SQL

这是我的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>

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.

现在我想起来了,也许我的代码在vb中存在问题。我会把它放在这里,请告诉我如何正确地将数据插入数据库。

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

1 个解决方案

#1


7  

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

如果要从代码中插入GUID,则应创建参数类型GUID

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

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

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

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

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

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

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

#1


7  

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

如果要从代码中插入GUID,则应创建参数类型GUID

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

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

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

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

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

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

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