简体   繁体   English

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

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

I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard" .我现在正在使用 Microsoft Visual Web Developer 2010 Express 创建一个网站,并且我使用了"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.然后我将"CustomerInfo"表的userId放入"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 .稍后,我需要将我的"Order"表链接到"CustomerInfo"表,所以我把我的orderId data type=uniqueidentifier Then, I plan insert my order details to the "Order" table but I have the problem of:然后,我计划将我的订单详细信息插入到"Order"表中,但我遇到了以下问题:

"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.现在想起来,可能是我的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

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)" 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不允许从数据类型sql_variant隐式转换为varbinary(max)。 使用CONVERT函数运行此查询。 ASP.NET SQL数据源 - Implicit conversion from data type sql_variant to varbinary(max) is not allowed. Use the CONVERT function to run this query. ASP.NET SQL DataSource System.Data.SqlClient.SqlException:不允许从数据类型sql_variant到uniqueidentifier的隐式转换。 - System.Data.SqlClient.SqlException: Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. 错误:不允许从数据类型varchar到varbinary(max)的隐式转换。 使用CONVERT函数运行此查询 - Error : Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query 不允许从数据类型nvarchar到二进制的隐式转换。 使用CONVERT函数运行此查询。 - Implicit conversion from data type nvarchar to binary is not allowed. Use the CONVERT function to run this query. 如何处理异常不允许从数据类型varchar隐式转换为varbinary(max)。 在将数据添加到数据库时 - how to handle the exception Implicit conversion from data type varchar to varbinary(max) is not allowed. while adding the data to database sql_variant 的大小限制超过 - size limit for the sql_variant exceeds 如何使用LINQ TO SQL(ASP.NET)显示sql_variant列中的值? - How to show values from a sql_variant column with LINQ TO SQL (ASP.NET)? 在数据库中插入uniqueidentifier类型。 转换错误 - Inserting uniqueidentifier type in database. Conversion error 编译的查询错误:没有从实体到&#39;System.Data.Objects.ObjectContext的隐式转换 - Compiled Query error: there is no implicit conversion from entities to 'System.Data.Objects.ObjectContext ASP.NET/SQL Server:从字符串转换为uniqueidentifier时转换失败 - ASP.NET/SQL Server: Conversion failed when converting from a character string to uniqueidentifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM