本地化语言中的ado.net sqlparameter [英] ado.net sqlparameter in localize language
本文介绍了本地化语言中的ado.net sqlparameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将波斯文本作为sqlparameter的值传递给asp.net/vb.net中的参数化查询
但是返回没有行 - 我在ssms中运行的相同查询 - 给出结果行。不明白哪里出错了。数据库已整理'Persian_100_ci_ai。
Vb.net:
I am passing Persian text as value of sqlparameter to a parameterized query in asp.net/vb.net
but returns no row - the same query i run in ssms - gives result rows. Don't understand where is the mistake. Database already collated 'Persian_100_ci_ai.
Vb.net:
Dim cmd As New SqlCommand
Dim dt As New DataTable
Dim dr As SqlDataReader
dt.TableName = "temp"
Try
If Not conn.State = ConnectionState.Closed Then conn.Close()
If conn.State = ConnectionState.Closed Then conn.Open()
cmd.Connection = conn
Dim qry As String = "Select * from users WHERE [Name]=@UserName AND [Pwd]=@Password"
cmd.commandtext = qry
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 50).Value = "ادمین"
cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = "ادمین"
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.HasRows Then
dt.Load(dr)
End If
Return dt
Catch ex As Exception
Return Nothing
Finally
dt = Nothing
cmd.Connection = Nothing
cmd.Parameters.Clear()
cmd.Dispose()
End Try
在SSMS中:
While in SSMS:
declare @UserName nvarchar(50) = 'ادمين'
declare @Password nvarchar(50)= 'ادمين'
select * from Users where [name]=@UserName and [Pwd] = @Password
在vb.net中有趣的部分我创建查询嵌入标准而不作为参数传递 - 它的工作原理:
Interesting part when in vb.net I create query embedding criteria and not passed as parameter - it works:
Dim cmd As New SqlCommand
Dim dt As New DataTable
Dim dr As SqlDataReader
Dim pLoginName As String = "ادمین"
Dim pPassword As String = "ادمین"
dt.TableName = "temp"
Try
If Not conn.State = ConnectionState.Closed Then conn.Close()
If conn.State = ConnectionState.Closed Then conn.Open()
cmd.Connection = conn
Dim qry As String ="Select * from users WHERE [Name]='" & pLoginName & "' AND [Pwd]='" & pPassword & "'"
cmd.CommandText = qry
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.HasRows Then
dt.Load(dr)
End If
Return dt
Catch ex As Exception
Return Nothing
Finally
dt = Nothing
cmd.Connection = Nothing
cmd.Parameters.Clear()
cmd.Dispose()
End Try
推荐答案
AddWithValue函数怎么样?即
What about the AddWithValue function? I.e.
cmd.Parameters.AddWithValue("@UserName", "ادمین")
作为PIEBALDconsult已经在这个中提到了[ ^ ]回答,删除此行:
As PIEBALDconsult had mentioned in this[^] answer, remove this line:
dt = Nothing
从最后
块。
它会摧毁你的桌子!
不要重新发布!
from Finally
block.
It destroys your table!
Do not repost!
全部谢谢你的回答。
我发现我错了。有两个数据库 - 英语和波斯语。我创建连接到相应数据库的连接类有bug,有时由于它连接到英文数据库i / o波斯语的bug。
现在我解决了错误。感谢我的同事Chintan Bilimoria指出 - 你是否选择了正确的数据库?
Thanks all for your answers.
I found where I was wrong. There were two databases - English and Persian. The connection class which I have created to connect to appropriate database has bug and sometime due to bug it was connecting to English database i/o Persian.
Now I resolved the bug. Thanks to my colleague Chintan Bilimoria, for pointing out - whether you are selecting right database?
这篇关于本地化语言中的ado.net sqlparameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文