参数化查询抛出错误

编程入门 行业动态 更新时间:2024-10-10 04:22:06
本文介绍了参数化查询抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的代码。我想使用参数将值传递给我的查询。

公共共享函数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( 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)

更多推荐

参数化查询抛出错误

本文发布于:2023-11-28 17:01:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   错误   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!