参数化查询抛出错误 [英] Parameterized query throws error
本文介绍了参数化查询抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码。我想使用参数将值传递给我的查询。
公共共享函数FillStates(ByVal strState As String)As DataSet
Dim ds As New DataSet
Dim da As New iDB2DataAdapter
Dim cn as iDB2Connection = CreateConnection()
尝试
如果strState.Length< = 0那么
Dim cm As New iDB2Command(SELECT * FROM DEVSUSH.CLSTATES,cn)
da.SelectCommand = cm
da.Fill(ds)
Else
Dim cm As New iDB2Command(SELECT * FROM DEVSUSH.CLSTATES Where STCODE ='@ strState,cn)
cm.Parameters.Add(@ strState,iDB2DbType.iDB2VarChar).Value = strState
da.SelectCommand = cm
da .Fill(ds)
结束如果
Catch ex As Exception
LogError.LogErrorIntoTextFile(例如,FillStates())
最后
cn.Cl ose()
结束尝试
返回ds
结束功能
我收到错误说
SQL语句文本无效。
格式异常。
有人可以帮我吗?
解决方案
取消报价!
昏暗 cm 作为 新 iDB2Command( SELECT * FROM DEVSUSH.CLSTATES其中STCODE ='@ strState,cn)成为
Dim cm 作为 新 iDB2Command(< span class =code-string> SELECT * FROM DEVSUSH.CLSTATES其中STCODE = @ strState,cn)
Here is my code. I want to use parameters to pass the values to my queries.
Public Shared Function FillStates(ByVal strState As String) As DataSet Dim ds As New DataSet Dim da As New iDB2DataAdapter Dim cn As iDB2Connection = CreateConnection() Try If strState.Length <= 0 Then Dim cm As New iDB2Command("SELECT * FROM DEVSUSH.CLSTATES", cn) da.SelectCommand = cm da.Fill(ds) Else Dim cm As New iDB2Command("SELECT * FROM DEVSUSH.CLSTATES Where STCODE='@strState", cn) cm.Parameters.Add("@strState", iDB2DbType.iDB2VarChar).Value = strState da.SelectCommand = cm da.Fill(ds) End If Catch ex As Exception LogError.LogErrorIntoTextFile(ex, "FillStates()") Finally cn.Close() End Try Return ds End Function
I get an error saying
The SQL statement text is not valid.
Format exception.
Could someone help me with this?
解决方案
Take off the quote!
Dim cm As New iDB2Command("SELECT * FROM DEVSUSH.CLSTATES Where STCODE='@strState", cn)Becomes
Dim cm As New iDB2Command("SELECT * FROM DEVSUSH.CLSTATES Where STCODE=@strState", cn)
这篇关于参数化查询抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文