我一直在看下面的代码,因为上帝知道多久,我似乎无法找到问题所在。我所看到的与所述错误有关的大多数讨论都表明来源是来自参数的拼写错误。但为什么撒谎,我看不到任何错字。有人能告诉我哪里出错了吗?
Dim dt As System.Data.DataTable = New System.Data.DataTable
dim sqlText as string
sqlText = "SELECT id AS privilegeID, " & _
"user_id AS userID, " & _
"module_id AS moduleID, " & _
"can_read AS canRead, " & _
"can_create AS canCreate, " & _
"can_update AS canUpdate, " & _
"can_delete AS canDelete, " & _
"is_deleted AS isDeleted, " & _
"is_active AS isActive " & _
"FROM base_user_privilege " & _
"WHERE " & _
"user_id=@userID AND " & _
"is_deleted=@isDeleted AND " & _
"is_active=@isActive"
SetDBCommand(sqlText)
DBCommand.Parameters.AddWithValue("@userID", userID)
DBCommand.Parameters.AddWithValue("@isDeleted", IsDeleted)
DBCommand.Parameters.AddWithValue("@isActive", IsActive)
dt = DBAction(sqlText, DBActionType.DataTable)
setdbcommand(sqltext)用于建立连接并且它可以工作,因此我认为错误不存在,因为它处理其他select和insert语句。
请协助。
感谢。
的修改
这是setdbcommand:
Public Sub SetDBCommand(ByVal sqlString As String)
If DBConnection.State = ConnectionState.Closed Then
DBConnection.Open()
End If
DBCommand = New SQLiteCommand(sqlString, DBConnection)
End Sub
适用于阅读器,但不适用于数据表
我已经意识到使用我的dataconnection函数,上述参数在通过 DataReader 传递时有效,但在通过 DataTable 传递时不起作用是否存在某些问题失踪?参数是否适用于DataTables?
Public Function DBAction(ByVal sqlText As String, ByVal ActionType As DBActionType) As Object
DBAction = Nothing
Select Case ActionType
Case DBActionType.DataReader
Return DBCommand.ExecuteReader
Case DBActionType.Dataset
Dim MyAdpater As New SQLiteDataAdapter(sqlText, DBConnection)
Dim myDataSet As New System.Data.DataSet
MyAdpater.Fill(myDataSet)
Return myDataSet
Case DBActionType.DataTable
Dim MyAdpater As New SQLiteDataAdapter(sqlText, DBConnection)
Dim MyDataTable As New System.Data.DataTable
MyAdpater.Fill(MyDataTable)
Return MyDataTable
Case DBActionType.executeNonQuery
DBCommand.ExecuteNonQuery()
Case DBActionType.ExecuteScalar
Dim ScalarItem As Object = DBCommand.ExecuteScalar
Return ScalarItem
Case Else
Throw New Exception("Error in DBACTION")
End Select
End Function
答案 0 :(得分:1)
创建SQLiteDataAdapter时,必须提供DBCommand作为参数。作为奖励,您不需要提供连接参数,因为您已经适当地设置了命令:
Public Function DBAction(ByVal sqlText As String, ByVal ActionType As DBActionType) As Object
DBAction = Nothing
Select Case ActionType
Case DBActionType.DataReader
Return DBCommand.ExecuteReader
Case DBActionType.Dataset
Dim MyAdpater As New SQLiteDataAdapter(DBCommand)
Dim myDataSet As New System.Data.DataSet
MyAdpater.Fill(myDataSet)
Return myDataSet
Case DBActionType.DataTable
Dim MyAdpater As New SQLiteDataAdapter(DBCommand)
Dim MyDataTable As New System.Data.DataTable
MyAdpater.Fill(MyDataTable)
Return MyDataTable
Case DBActionType.executeNonQuery
DBCommand.ExecuteNonQuery()
Case DBActionType.ExecuteScalar
Dim ScalarItem As Object = DBCommand.ExecuteScalar
Return ScalarItem
Case Else
Throw New Exception("Error in DBACTION")
End Select
End Function
顺便说一句,由于我在您发布的代码中没有看到它们,请确保您正确处理SQLiteCommand
和SQLiteDataAdapter
个对象。连接应该关闭,然后在完成后处理。
答案 1 :(得分:0)
您的问题是您要将参数添加到DBCommand,但在case语句中,您的适配器从不使用DBCommand,它只获取您传入的字符串(sqlText)。
一种可能的解决方案是代替传递sqlText作为参数传递整个命令,并将其分配给MyAdapter.SelectCommand
Function DBAction( inCommand As SQLiteCommand, ActionType As DBActionType)