通过ASP.NET中的内联C#将字符串转换为GUID
是否可以在ASP.NET中使用内联C#将字符串转换为GUID?在本例中,我希望为通过ASP.NET中的内联C#将字符串转换为GUID,c#,asp.net,guid,C#,Asp.net,Guid,是否可以在ASP.NET中使用内联C#将字符串转换为GUID?在本例中,我希望为CustomerId传入一个字符串,并将其格式化为GUID,因为存储过程需要唯一标识符 <asp:SqlDataSource ID="myDataSOurce" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="GetUserData" SelectCommandT
CustomerId
传入一个字符串,并将其格式化为GUID,因为存储过程需要唯一标识符
<asp:SqlDataSource ID="myDataSOurce" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="GetUserData" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="<% new Guid(customerId).ToString() %>" Name="CustomerId" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
尝试此操作时,我从SQL Server收到一个错误:
将数据类型nvarchar转换为uniqueidentifier时出错
<asp:SqlDataSource ID="myDataSOurce" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="GetUserData" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="<% new Guid(customerId).ToString() %>" Name="CustomerId" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
试试这个:
<asp:Parameter DefaultValue="<% Guid.NewGuid() %>" Name="CustomerId" Type="String" />
</SelectParameters>
是否可以修改存储过程以接受字符串,然后在SQL代码中将字符串强制转换为uniqueidentifier。如果是,请检查此项 对于这个答案,有几个注释可以用更少的代码实现同样的效果。 然而,如果这是不可能的,看看这是否有效
<asp:SqlDataSource ID="myDataSOurce" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="GetUserData" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="<% System.Guid.Parse(customerId) %>" Name="CustomerId" DbType="Guid" />
</SelectParameters>
当然,您可以在C#中将字符串转换为Guid。您的错误似乎是一个类型问题。我怀疑在这里将您的参数类型声明为字符串会混淆它。最初尝试使用
新Guid(customerId)
,但收到相同的错误。new
关键字会成为问题吗?可能会使用绑定表达式DefaultValue='
,但为什么要转换为Guid,然后调用它上的ToString,然后它基本上又变成了customerId
。我想.ToString()
会给我一个Guid中有格式的字符串(即破折号)。我还尝试了绑定语法,但它被拒绝了,因为控件没有实现DataBind
我想您应该使用Type=“Object”
作为guid。(未确认)相同的错误。由于某些原因,SQL不接受它作为唯一标识符