我应该打电话给专家:D。帮我看看这个漂亮的代码:)更正此SQL查询:错误“Microsoft Jet数据库引擎无法找到输入表或查询'IF'”
数据库:
“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
第一个程序是一个包装,以将数据添加到数据库字段(如果数据doent存在) 但随着新的数据已经存在更新的行。
输入:
表=表名
列=将由逗号更新分离colomns的名称(练习1: “ID”,实例2: “ID,标题,...”)
记录()=字符串数组,代表新的值(多值与逗号分隔)
OK,增加值数据库之前,我们应该检查是否有行存在这个值:)
要做到这一点,创建一个存储过程一种快速处理数据库的最佳方法。
所以......现在的问题是,在运行时,OLEDB小姐抛出这个错误:
Microsoft Jet数据库引擎无法找到输入表或查询“IF” ....
在此先感谢:D
你移植的SQL Server应用程序来访问/喷气? – Constantin 2010-08-27 13:50:14