根据这个帖子,Action
是Access数据库引擎的一个保留字。因此,您需要使用QuotePrefix和QuoteSuffix,如下面的代码所示。此外,任何具有Dispose
方法的对象都应该使用using 语句或 call Dispose
。
如果您希望使用OleDbCommandBuilder将一行插入您的 Access 数据库:
创建表(名称:Autolog)
![在此处输入图像描述](https://i.stack.imgur.com/6jJiq.png)
CREATE TABLE Autolog(ID AUTOINCREMENT not null primary key,
[UserID] varchar(25),
[Action] varchar(125),
[RemoteComputerName] varchar(50),
[RemoteIP] varchar(50));
然后,尝试以下(已经过测试):
Private _connectionStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Freightmasterbe\Freightmaster.accdb;"
Public Function TblAutologInsert(ByVal LogEntry As String, ByVal UserId As Integer) As Integer
Dim dt As DataTable = New DataTable()
Dim remoteComputerName As String = My.Computer.Name
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim remoteIP = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
Using cnn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cnn.Open()
Using da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * from Autolog order by Id", cnn)
'get data from database
da.Fill(dt)
Using cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
'needed if database column name contains either spaces or is a reserved word
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
'new row
Dim row As DataRow = dt.NewRow()
'set values
row.Item("UserID") = UserId
row.Item("Action") = LogEntry
row.Item("RemoteComputerName") = remoteComputerName
row.Item("RemoteIP") = remoteIP
'add row to DataTable
dt.Rows.Add(row)
'get insert command
da.InsertCommand = cb.GetInsertCommand(True)
'Debug.WriteLine("da.InsertCommand.CommandText: " & da.InsertCommand.CommandText)
'update database and return number of rows affected
Return da.Update(dt)
End Using
End Using
End Using
End Function
用法:
Dim rowsAffected As Integer = 0
'insert test data
rowsAffected += HelperAccess.TblAutologInsert("test 1", 1)
rowsAffected += HelperAccess.TblAutologInsert("test 2", 2)
rowsAffected += HelperAccess.TblAutologInsert("test 3", 3)
Debug.WriteLine("rowsAffected: " & rowsAffected.ToString())
这是一些通过“Id”更新记录的代码。
Public Function TblAutologUpdate(LogEntry As String, UserId As Integer, id As Integer) As Integer
Dim dt As DataTable = New DataTable()
Dim remoteComputerName As String = My.Computer.Name
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim remoteIP = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
Using cnn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cnn.Open()
Using cmd As OleDbCommand = New OleDbCommand("SELECT * from Autolog where Id = ?", cnn)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
'add parameters
With cmd.Parameters
.Add("!id", OleDbType.Integer).Value = id
End With
Using da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
'get data from database
da.Fill(dt)
Using cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
'needed if database column name contains either spaces or is a reserved word
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
If dt.Rows.Count >= 0 Then
'set value
Dim row As DataRow = dt.Rows(0)
'set values
row.Item("UserID") = UserId
row.Item("Action") = LogEntry
row.Item("RemoteComputerName") = remoteComputerName
row.Item("RemoteIP") = remoteIP
'get update command
da.UpdateCommand = cb.GetUpdateCommand()
'update database and return number of rows affected
Return da.Update(dt)
End If
End Using
End Using
End Using
End Using
End Function
用法:
Dim rowsAffected As Integer = 0
'update
rowsAffected += HelperAccess.TblAutologUpdateById("test 2b", 2, 2)
Debug.WriteLine("rowsAffected: " & rowsAffected.ToString())
资源