[英]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.