2012-07-07 186 views
3

你好我有一個數據庫,我加載到vb.net應用程序Datagridview。 它加載正常,但是當我嘗試保存日期不起作用。 這裏是代碼當新行傳遞的DataRow集合Datagridview保存更改爲數據庫vb.net

Private myConString As String 
Private con As OleDbConnection = New OleDbConnection 
Private Dadapter As OleDbDataAdapter 
Private DSet As DataSet 
Private DSet2 As DataSet 
Private ConCMD As OleDb.OleDbCommand 

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\Database31.accdb" 
    con.ConnectionString = myConString 
    con.Open() 
    Dadapter = New OleDbDataAdapter("select * from Table1", con) 
    DSet = New DataSet 
    Dadapter.Fill(DSet, "Table1") 
    DataGridView1.DataSource = DSet.Tables("Table1") 
    con.Close() 
End Sub 


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    con.Open() 
    Dadapter.Update(DSet, "Table1") 
    DSet.AcceptChanges() 
    con.Close() 
End Sub 

更新需要有效的InsertCommand。 我該怎麼辦?

的accessdatabase 3個colmuns ID是主鍵 ID字段1字段2

回答

4

所以,你必須定義一個InsertCommandDataAdapter

側面說明:該生產線DSet.AcceptChanges()是多餘的,因爲前行Dadapter.Update將調用隱含地表示爲AcceptChanges

您應該使用using-statement來執行任何類似Connection的IDisposable。即使發生異常,也會隱含地調用Dispose(即關閉連接)。

所以更換:

con.Open() 
Dadapter.Update(DSet, "Table1") 
DSet.AcceptChanges() 
con.Close() 

Using con = New OleDbConnection(myConString) 
    con .Open() 
    Dadapter.Update(DSet, "Table1") 
End Using 
+0

謝謝,但仍然是什麼插入命令?!我的數據庫只有3個字段ID Field1 Field2..so我的插入命令應該如何? – FPGA 2012-07-08 00:10:01

+0

編輯我的答案,爲[OleDbDataAdapter.InsertCommand]提供msdn鏈接(http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.insertcommand%28v=vs.100%29。 ASPX)。有一個sql INSERT語句的例子。 – 2012-07-08 00:13:48

+0

偉大..現在假設我填充了數據網格視圖2000個新的值與不同領域的循環..我想保存這是什麼是最好的方法來做到這一點?插入應該在循環期間而不是在循環之後? – FPGA 2012-07-08 00:26:09

0

您需要從DataGrid讀回的數據集

con.Open() 
DSet = DataGridView1.DataSource '<<<<<<<<<<<<<<<<<<<<<<< 
Dadapter.Update(DSet, "Table1") 
DSet.AcceptChanges() 
con.Close() 
0

爲4型數據庫我的VB.NET代碼(從DataGridView到數據庫的更新信息)

Private Sub sqldb_savedata() 
    Dim connectionString As String = "Server='" & sql_server & "';Database='" & sql_database & "';User Id='" & sql_user & "';Password='" & sql_pass & "'" 
    Dim sqlCon = New SqlConnection(connectionString) 
    If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open() 
    Dim SQLAdapter = New SqlDataAdapter("SELECT * FROM clinics", sqlCon) 
    Dim SQLDataSet As New DataSet 
    Dim myTable = DataGridViewClinic.DataSource 
    Dim cmdbuilder As New SqlCommandBuilder(SQLAdapter) 
    SQLAdapter.Update(myTable, "clinics") 
    MsgBox("Updated!", MsgBoxStyle.OkOnly, "") 
End Sub 

Private Sub mysqldb_savedata() 
    Dim connectionString As String = "Server='" & mysql_server & "';Database='" & mysql_database & "';User Id='" & mysql_user & "';Password='" & mysql_pass & "'" 
    Dim sqlCon = New MySqlConnection(connectionString) 
    If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open() 
    Dim SQLAdapter = New MySqlDataAdapter("SELECT * FROM clinics", sqlCon) 
    Dim SQLDataSet As New DataSet 
    Dim myTable = DataGridViewClinic.DataSource 
    Dim cmdbuilder As New MySqlCommandBuilder(SQLAdapter) 
    SQLAdapter.Update(myTable, "clinics") 
    MsgBox("Updated!", MsgBoxStyle.OkOnly, "") 
End Sub 

Private Sub firebirddb_savedata() 
    Dim connectionString As String = "Database='" & firebird_server & "';User=SYSDBA;Password=masterkey;Dialect=3;ServerType=1" 
    Dim sqlCon = New FirebirdSql.Data.FirebirdClient.FbConnection(connectionString) 
    If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open() 
    Dim SQLAdapter = New FirebirdSql.Data.FirebirdClient.FbDataAdapter("SELECT * FROM clinics", sqlCon) 
    Dim SQLDataSet As New DataSet 
    Dim myTable = DataGridViewClinic.DataSource 
    Dim cmdbuilder As New FirebirdClient.FbCommandBuilder(SQLAdapter) 
    SQLAdapter.Update(myTable, "clinics") 
    MsgBox("Updated!", MsgBoxStyle.OkOnly, "") 
End Sub 

Private Sub localdb_savedata() 
    DBconn = New SqlCeConnection("Data Source=Data Source=|DataDirectory|\Database.sdf") 
    If (DBconn.State = ConnectionState.Closed) Then DBconn.Open() 
    Dim SQLAdapter = New SqlCeDataAdapter("SELECT * FROM clinics", DBconn) 
    Dim SQLDataSet As New DataSet 
    Dim myTable = DataGridViewClinic.DataSource 
    Dim cmdbuilder As New SqlCeCommandBuilder(SQLAdapter) 
    SQLAdapter.Update(myTable, "clinics") 
    MsgBox("Updated!", MsgBoxStyle.OkOnly, "") 
End Sub 
相關問題