从vb程序插入查询在访问数据库中不添加任何记录。
database connection class-->
Imports System.Data.OleDb
Public Class Data
Shared dbconnection As OleDbConnection
Shared dbcommand As OleDbCommand
Shared dbadapter As OleDbDataAdapter
Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL\Documents\Visual Studio 2013\Projects\Chakana saving and credit\Chakana saving and credit\Chakana.mdb"
Shared dbtable As DataTable
Public Shared Function loadData(query As String, Optional ReturnValue As Boolean = False)
dbconnection = New OleDbConnection(connectionString)
dbadapter = New OleDbDataAdapter
dbtable = New DataTable()
dbcommand = New OleDbCommand(query)
dbcommand.CommandType = CommandType.TableDirect
dbadapter.SelectCommand = dbcommand
dbadapter.SelectCommand.Connection = dbconnection
dbconnection.Open()
'MsgBox("query lexecute")
If ReturnValue = True Then
dbadapter.Fill(dbtable)
Return dbtable
Else
Return 0
End If
End Function
结束班
函数调用传递参数查询
query = "INSERT INTO CustomerInfo (ClientId,FirstName) VALUES ('23','abce')"
Data.loadData(query)
对函数传递SELECT查询的其他调用工作正常但是当我尝试插入查询并且之后从访问中打开数据库时它没有结果
答案 0 :(得分:1)
退房 OleDbTransaction.Commit Method ()和DbDataAdapter.Update Method显示如何在连接中打开和提交事务。
答案 1 :(得分:0)
您没有执行DataTable
来填充DataAdapter
(因为默认情况下returnValue设置为false)加上您既不需要定义returnValue也不需要Imports System.Data.OleDb
Public Class Data
Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL\Documents\Visual Studio 2013\Projects\Chakana saving and credit\Chakana saving and credit\Chakana.mdb"
Public Shared Function loadData(query As String) As DataTable
Dim dbtable As New DataTable
Dim dbcommand As New OleDbCommand(query)
dbcommand.Connection = New OleDbConnection(connectionString)
dbcommand.CommandType = CommandType.TableDirect
dbcommand.Connection.Open()
dbtable.Load(dbcommand.ExecuteReader())
dbcommand.Connection.Close()
Return dbtable
End Function
End Class
填表。我建议将您的功能更改为:
{{1}}