SELECT [FirstName], [LastName] FROM [WorkersView] WHERE ([FirstName] LIKE '%' + N'علی' + '%' ) AND ([LastName] LIKE '%' + N'مسروری' + '%')
它的工作非常好!并给了我想要的结果!
但我希望将参数发送到查询中,如下所示:
SELECT [FirstName], [LastName] FROM [WorkersView] WHERE ([FirstName] LIKE '%' + N@FirstName + '%' ) AND ([LastName] LIKE '%' + N@LastName + '%')
没有用!
我也试过这段代码:
SELECT [FirstName], [LastName] FROM [WorkersView] WHERE ([FirstName] LIKE N'%' + @FirstName + '%' ) AND ([LastName] LIKE N'%' + @LastName + '%')
也不起作用!并且没有结果返回!
我的校对是Persian_100_CI_AI。和nvarchar
中列的所有数据类型同时,我在asp.net中有这段代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Width="645px"> <Columns> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ResamanConnectionString %>" SelectCommand="SELECT [FirstName], [LastName] FROM [WorkersView] WHERE (([FirstName] LIKE N'%' + @FirstName + N'%') AND ([LastName] LIKE N'%' + @LastName + N'%'))" > <SelectParameters> <asp:ControlParameter ControlID="firstNameTextBox" Name="FirstName" PropertyName="Text" Type="String" /> <asp:ControlParameter ControlID="lastNameTextBox" Name="LastName" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource>
如何使用带有N前缀的代码发送参数?
答案 0 :(得分:2)
首先,将查询写为:
SELECT [FirstName], [LastName]
FROM [WorkersView]
WHERE ([FirstName] LIKE N'%' + @FirstName + N'%' ) AND ([LastName] LIKE N'%' + @LastName + N'%')
参数应设置为:
DECLARE @FirstName NVARCHAR(255) = N'علی';
DECLARE @LastName NVARCHAR(255) = N'مسروری';
将字符串声明为nvarchar()
而不是varchar()
是解决方案中最重要的部分。
答案 1 :(得分:0)
“nvarchar”作为数据类型需要将数据保存为unicode, 并将前缀设置为 N'' + @parameter + N''