通过Visual Basic更新访问表 [英] Access table update by visual basic
问题描述
以下代码已执行,但尚未更新数据,请建议如何解决
the following code is executed but has not updated the data, please advice how to fix it
推荐答案
最常见的解决方案是确保数据库(如果在解决方案资源管理器中显示)将复制到输出文件夹"属性设置为复制"如果较新.接下来,确保您指向正确的位置.
The most common solution is to ensure the database if shown in Solution Explorer has Copy to output folder property set to copy if newer. Next, ensure you are pointing to the proper location.
接下来,
以整数= com.ExecuteNonQuery的影响
Dim Affected As Integer = com.ExecuteNonQuery
受影响的将显示更新或未更新多少行.
Affected will show how many rows were updated or not.
读取和更新的基本示例.标识符是一个自动递增的主键
A basic example for read and update. Identifier is an auto incrementing primary key
Public Class Operations
Public Property HasErrors As Boolean
Public Property ErrorMessage As String
Public Property ExceptionThrown As Exception
Public Property CustomerDataTable As DataTable
Private ConnectionBuilder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = IO.Path.Combine(IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "Database1.accdb"))
}
Public Function LoadCustomers() As Boolean
Using cn As New OleDb.OleDbConnection With {.ConnectionString = ConnectionBuilder.ConnectionString}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cmd.CommandText =
<SQL>
SELECT
Identifier,
CompanyName,
ContactName,
ContactTitle,
Country
FROM
Customer
</SQL>.Value
CustomerDataTable = New DataTable
Try
cn.Open()
CustomerDataTable.Load(cmd.ExecuteReader)
CustomerDataTable.Columns("Country").ColumnMapping = MappingType.Hidden
Return True
Catch ex As Exception
HasErrors = True
ExceptionThrown = ex
End Try
Return False
End Using
End Using
End Function
Public Function UpdateRecord(ByVal CustomerIdentifier As Integer, ByVal ContactName As String) As Boolean
Using cn As New OleDb.OleDbConnection With {.ConnectionString = ConnectionBuilder.ConnectionString}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cmd.CommandText = "UPDATE Customer SET ContactName = @ContactName WHERE Identifier=@Identifier;"
cmd.Parameters.AddWithValue("@ContactName", ContactName)
cmd.Parameters.AddWithValue("@Identifier", CustomerIdentifier)
cn.Open()
Return cmd.ExecuteNonQuery = 1
End Using
End Using
End Function
End Class
请注意我是如何构造连接和命令的.使用using构造处理对象的处置时,按原样创建的理由为零.
Note how I am construct the connection and command. There is zero reasons to create as you are as the using construct handles the disposal of the objects.
参数是顺序位置,但是按上面的方法命名它们并不是一个坏主意.
Parameters are ordinal position but it's not a bad idea to name them as done above.
完整示例 https://1drv.ms/u/s!AtGAgKKpqdWjiCh4NZ-1X2cxLV-Q
这篇关于通过Visual Basic更新访问表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!