将 Unicode 字符串从 C# 发送到 SQL Server 2008 DB

发布于 2024-12-10 22:41:25 字数 2140 浏览 0 评论 0原文

我想将一些 unicode 字符串(例如“Í”)发送到我的 SQL 数据库存储过程。 我的代码是:

SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");

SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text; 



command.CommandText = s;
connection.Open();



SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
Common.CommonPersonSerchResult res = new Common.CommonPersonSerchResult();

res.ID = (int)reader.GetValue(0);
res.FirstName = reader.GetValue(1).ToString();
res.LastName = reader.GetValue(2).ToString();
res.FatherName = reader.GetValue(3).ToString();
res.NationalCode = (int)reader.GetValue(4);
res.ShenasnameCode = (int)reader.GetValue(5);
res.BirthDate = reader.GetValue(6).ToString();
res.State = reader.GetValue(7).ToString();
res.City = reader.GetValue(8).ToString();
res.PostalCode = (int)reader.GetValue(10);
res.SportType = reader.GetValue(11).ToString();
res.SportStyle = reader.GetValue(12).ToString();
res.RegisterType = reader.GetValue(13).ToString();
res.Ghahremani = reader.GetValue(14).ToString();

SerchResult.Add(res);

}

connection.Close();

但我看不到任何结果,但我知道有一些行可以显示

这是我的存储过程:

USE [Khane]
GO
/****** Object:  StoredProcedure [dbo].[QuickSerch]    Script Date: 10/19/2011 18:31:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[QuickSerch] 
@item nvarchar(300)
AS
BEGIN    
select * from PersonsDataTbl 
where 
Name like '%@item%' or 
LastName like '%@item%' or
FatherName like '%@item%' or
NationalCode like '%@item%' or
ShenasnameCode like '%@item%' or
BirthDate like '%@item%' or
State like '%@item%' or
City like '%@item%' or
Address like '%@item%' or
PostalCode like '%@item%' or
SportType like '%@item%' or
SportStyle like '%@item%' or
RegisterType like '%@item%' or
Ghahremani like '%@item%' 
END

I want to send some unicode strings (for example "ش") to my SQL DataBase stored procedure.
and my code is :

SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");

SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text; 



command.CommandText = s;
connection.Open();



SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
Common.CommonPersonSerchResult res = new Common.CommonPersonSerchResult();

res.ID = (int)reader.GetValue(0);
res.FirstName = reader.GetValue(1).ToString();
res.LastName = reader.GetValue(2).ToString();
res.FatherName = reader.GetValue(3).ToString();
res.NationalCode = (int)reader.GetValue(4);
res.ShenasnameCode = (int)reader.GetValue(5);
res.BirthDate = reader.GetValue(6).ToString();
res.State = reader.GetValue(7).ToString();
res.City = reader.GetValue(8).ToString();
res.PostalCode = (int)reader.GetValue(10);
res.SportType = reader.GetValue(11).ToString();
res.SportStyle = reader.GetValue(12).ToString();
res.RegisterType = reader.GetValue(13).ToString();
res.Ghahremani = reader.GetValue(14).ToString();

SerchResult.Add(res);

}

connection.Close();

But I can't see any results but I know There is some rows can be show

this is my stored procedure:

USE [Khane]
GO
/****** Object:  StoredProcedure [dbo].[QuickSerch]    Script Date: 10/19/2011 18:31:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[QuickSerch] 
@item nvarchar(300)
AS
BEGIN    
select * from PersonsDataTbl 
where 
Name like '%@item%' or 
LastName like '%@item%' or
FatherName like '%@item%' or
NationalCode like '%@item%' or
ShenasnameCode like '%@item%' or
BirthDate like '%@item%' or
State like '%@item%' or
City like '%@item%' or
Address like '%@item%' or
PostalCode like '%@item%' or
SportType like '%@item%' or
SportStyle like '%@item%' or
RegisterType like '%@item%' or
Ghahremani like '%@item%' 
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2024-12-17 22:41:25

您的存储过程代码应该是:

LIKE '%' + @item + '%'

否则您将查找包含文字字符串“@item”的值。

另外,请注意,如果表中有大量行,您的代码不太可能执行得很好。在 OR 语句和前导通配符之间,它将无法使用任何索引。您可能想研究全文搜索。

Your stored procedure code should be:

LIKE '%' + @item + '%'

Otherwise you're looking for values that contain the literal string "@item".

Also, be aware that your code isn't likely to perform very well if you have a large number of rows in your table. Between the OR statements and the leading wildcards it won't be able to make use of any indexes. You might want to look into fulltext searching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文