【问题标题】:Using user.identity.Name in SQL statement在 SQL 语句中使用 user.identity.Name
【发布时间】:2015-09-25 22:07:49
【问题描述】:

在我的 ASP.NET 应用程序中,我有以下有效的 SQL:

<asp:SqlDataSource ID="ISESDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [id], [Word], [Definition], [Example] FROM [gridData] WHERE [Strategy]='Vocabulary'">

但是,我需要在 Where 子句中添加用户 ID 检查,并希望使用 user.identity.Name 来执行检查。我尝试了以下方法,但它不起作用:

<asp:SqlDataSource ID="ISESDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [id], [Word], [Definition], [Example] FROM [gridData] WHERE [userid]= /'" + user.identity.Name + "/' AND [Strategy]='Vocabulary'">

这是错误:

解析器错误 说明:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当地修改您的源文件。

解析器错误消息:服务器标签格式不正确。

来源错误:

Line 46:             <asp:SqlDataSource ID="ISESDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [id], [Word], [Definition], [Example] FROM [gridData] WHERE [userid]=/'" + User.Identity.Name + "/' AND [Strategy]='Vocabulary'">

我做错了什么?

【问题讨论】:

  • 为什么用/s 包围用户名?
  • 已删除并收到以下错误:解析器错误说明:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当地修改您的源文件。解析器错误消息:服务器标记格式不正确。第 46 行:minimal reproducible example FROM [ gridData] WHERE [userid]='" + User.Identity.Name + "' AND [Strategy]='Vocabulary'">

标签: sql asp.net


【解决方案1】:

我认为您需要重新考虑您的方法。与其尝试在 asp:SqlDataSource 元素中“硬编码”“user.Identity.Name”属性,不如尝试创建一个参数来保存该值:

1) 将 SelectCommand 中的“user.Identity.Name”替换为参数的标记,例如“@Name”。

2) 然后为 asp:SqlDataSource 定义一个 SelectParameter 元素,该元素的 Name 属性为“Name”。将参数的 Type 属性设置为“user.Identity.Name”的数据类型。

3) 然后,您可以在 SqlDataSource 的 Selecting 事件的事件处理程序中以编程方式定义要选择的值。

这是一个有效的例子。以下代码sn-p来自aspx页面:

<asp:GridView ID="myGridView" runat="server" DataSourceID="myDataSource" DataKeyNames="ID"></asp:GridView>
<asp:SqlDataSource ID="myDataSource" runat="server"
    SelectCommand="SELECT * FROM [Users] WHERE [Name] = @Name"
    ConnectionString='Data Source=(LocalDB)\v11.0;AttachDbFilename="c:\users\windowsLogin\documents\visual studio 2012\Projects\WebApplication1\WebApplication1\App_Data\Database1.mdf";Integrated Security=True'>
    <SelectParameters>
        <asp:Parameter Name="Name" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

然后,在代码隐藏中,您可以在运行时根据任何重要标准以编程方式定义您的参数。 (这里,我只是将值 '2' 分配给 @Name 参数;您可以将其替换为 'user.Identity.Name'。)

Private Sub myDataSource_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles myDataSource.Selecting
    e.Command.Parameters("@Name").Value = 2
End Sub

我希望这很清楚...如果没有,请参阅 MSDN 上的文档。这里有几页可以帮助您入门:

SqlDataSource.SelectCommand Property

SqlDataSourceSelectingEventArgs Class

更一般地说,

Using Parameters with Data Source Controls for Filtering

【讨论】:

  • 谢谢,詹姆斯。你摇滚!
  • 经过深思熟虑的答案。谢谢。
【解决方案2】:

转义字符串中的特殊字符是使用反斜杠'\'而不是正斜杠'/'。试试这个,看看它是否有效:

<asp:SqlDataSource ID="ISESDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [id], [Word], [Definition], [Example] FROM [gridData] WHERE [userid]= \'" + user.identity.Name + "\' AND [Strategy]='Vocabulary'">

更新:正如 sroonet 测试的那样,答案并非如此简单。实际答案是在 SelectCommand 中使用参数化查询。在 SelectCommand 中使用参数化查询的详细信息,请参考以下网页: https://msdn.microsoft.com/en-us/library/z72eefad.aspx

【讨论】:

  • 进行了建议的更改,现在得到:解析器错误说明:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当地修改您的源文件。解析器错误消息:服务器标记格式不正确。第 46 行:minimal reproducible example FROM [ gridData] WHERE [userid]=\'" + User.Identity.Name + "\' AND [Strategy]='Vocabulary'" >
  • 好的,我做了一些研究,问题没那么简单。基本上,您不能按照此链接 (stackoverflow.com/questions/1112371/…) 在 SelectCommand 属性中进行字符串连接。您可能想尝试的是关注这篇文章:msdn.microsoft.com/en-us/library/z72eefad.aspx 使用参数化查询。
猜你喜欢
  • 1970-01-01
  • 2015-05-09
  • 2012-01-17
  • 2020-10-13
  • 2018-05-12
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
相关资源
最近更新 更多