2016-01-07 172 views
0

從vb程序插入查詢不會在訪問數據庫中添加記錄。SQL插入沒有結果

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

您還沒有執行過 – Steve

+0

那麼爲什麼選擇查詢(其它形式)執行正確調用這個非常相同的功能,當查詢? query =「SELECT Password,AccountType FROM Cooperative WHERE UserName =」+「'」+用戶名+「'」 tdata = Data.loadData(query,True) –

+0

您將更新查詢設置爲select命令。 –

回答

0

您還沒有執行你的DataAdapter填補DataTable(因爲了returnvalue默認設置爲false),再加上你不要也不需要定義的returnValue也不DataAdapter只是填充表。我推薦給你的功能更改爲類似這樣:

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