更正此SQL查询:错误“ Microsoft Jet数据库引擎无法找到输入表或查询'如果'
-
01-10-2019 - |
题
我应该说嗨,专家:d。帮助我使用这个漂亮的代码:)
数据库:
“ ID(主键)” | “标题”
0 | “ title1”
1 | “ title2”
2 | “ title3”
3 | “ title4”
Sub AddRecord(ByVal Table As String, ByVal Columns As String, ByVal Record() As String)
Dim SubDir As String = ""
Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & Table, connectionString)
Dim command As OleDbCommand
Dim tbCorrectSyntax = ""
Using connection As New OleDbConnection(connectionString)
Try
connection.Open()
Dim Commandtxt As String = ""
For i = 0 To Record.GetUpperBound(0)
If Record(i).IndexOf(",") > 0 Then
Dim tmpRec() As String = Nothing
Dim cols() As String = Nothing
tmpRec = Record(i).Split(",")
cols = Columns.Split(",")
For j = 0 To tmpRec.GetUpperBound(0)
tbCorrectSyntax &= cols(j) & " = '" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ")
Next
End If
Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & " = " & Record(i), tbCorrectSyntax)
Commandtxt = "IF OBJECT_ID ( 'InsertOrUpdateItem', 'P' ) IS NOT NULL " & _
"DROP PROCEDURE InsertOrUpdateItem " & _
"GO " & _
"CREATE PROCEDURE InsertOrUpdateItem " & _
"AS " & _
"IF (EXISTS (SELECT * FROM " & Table & _
" WHERE " & txtCorrect & "))" & _
" begin " & _
"UPDATE (" & Table & ") " & _
txtCorrect & _
" WHERE " & txtCorrect & " " & _
" End " & _
" else " & _
" begin " & _
" INSERT INTO " & Table & " (" & Columns & ") " & _
" VALUES (" & Record(i) & ")" & _
" End " & _
"End "
'Commandtxt = "INSERT INTO " & Table & " (" & Columns & ") VALUES (" & Record(i) & ")"
command = New OleDbCommand(Commandtxt, connection)
command.CommandType = CommandType.StoredProcedure
command.ExecuteNonQuery()
Next
Catch ex As Exception
msgbox(ex.Message)
Finally
connection.Close()
End Try
End Using
End Sub
Function connectionString() As String
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBdir & ";User Id=admin;Password=;"
End Function
第一个过程是一个包装器,可以将数据添加到数据库字段(如果存在数据存在),但已将其更新为已有新数据的行。
输入:
表= tableName
列= Colomn的名称将被逗号分开(EX1:“ ID”,EX2:“ ID,Title,...”)
record()=表示新值的字符串数组(多个值用逗号分隔)
好的,在将值添加到数据库之前,我们应该检查是否存在此值的行是否存在:)
为此,创建存储过程是快速处理数据库的最佳方法。
所以...现在的问题是,在运行时,OLEDB小姐抛出了这个错误:
Microsoft Jet数据库引擎找不到输入表或查询'如果'....
提前致谢
解决方案 2
我找到了解决我的问题的解决方案(有一些净研究:))哈哈哈哈我很高兴
无论如何,最初的问题是:“如果存在,如何更新数据库中的记录”,因此我尝试在数据库中创建和存储一个存储过程...但是... :)
然后我找到了一个有趣的方法:OLEDBCOMMAND类的执行级
它只是根据SQL输入返回值:在我使用的下面示例中,如果存在RECOD,则返回索引(主键)。因此,让我们开始:
Function GetRecordIndex(ByVal Table As String, ByVal Columns As String, ByVal Record As String) As String
Dim Commandtxt As String = ""
Dim Command As OleDbCommand
Dim tbCorrectSyntax As String = ""
Dim tbCorrectSyntaxAND As String = ""
Using connection As New OleDbConnection(connectionString)
Try
connection.Open()
If Record.IndexOf(",") > 0 Then
Dim tmpRec() As String = Nothing
Dim cols() As String = Nothing
tmpRec = Record.Split(",")
cols = Columns.Split(",")
For j = 0 To tmpRec.GetUpperBound(0)
tbCorrectSyntax &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ")
tbCorrectSyntaxAND &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " AND ")
Next
End If
Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & "=" & Record, tbCorrectSyntax)
Dim txtCorrectAND As String = IIf(tbCorrectSyntaxAND = "", Columns & "=" & Record, tbCorrectSyntaxAND)
Commandtxt = "SELECT * FROM " & Table & " WHERE " & txtCorrectAND
Command = New OleDbCommand(Commandtxt, connection)
Dim RecordIndex As String = Command.ExecuteScalar
Return RecordIndex
Catch ex As Exception
Return Nothing
Finally
connection.Close()
End Try
End Using
End Function
和以前一样,列参数可以是单个数据库列,也可以是用逗号分隔的多个列。与表示每列内部数据的记录相同的东西
感谢您的帮助
Fadelovesky
其他提示
command.CommandType = CommandType.StoredProcedure
您声称您正在运行存储的proc(命令文本将是现有的sproc的名称)。您实际上给它一个SQL命令要直接执行。命令类型应为文本;
不隶属于 StackOverflow