3

您好,我有一个数据库,我在 vb.net 应用程序中加载到 Datagridview。它加载正常,但是当我尝试保存日期时它不起作用。这是代码

    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

当传递带有新行的 DataRow 集合时,更新需要有效的 InsertCommand。我应该做些什么?

accessdatabase 3 colmuns 和 ID 是主键 ID Field1 Field2

4

3 回答 3

4

所以你必须InsertCommand为你定义一个DataAdapter

旁注:该行DSet.AcceptChanges()是多余的,因为前一行Dadapter.UpdateAcceptChanges隐式调用。

您应该使用using-statement任何实现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
于 2012-07-07T23:56:49.273 回答
0

您需要从数据网格中读回数据集

con.Open()
DSet = DataGridView1.DataSource  '<<<<<<<<<<<<<<<<<<<<<<<
Dadapter.Update(DSet, "Table1")
DSet.AcceptChanges()
con.Close()
于 2014-05-07T22:12:57.927 回答
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
于 2015-09-24T17:28:49.847 回答