无法通过DataGridView更新数据库 - 没有错误

时间:2014-03-27 09:25:45

标签: ms-access visual-studio-2012 datagridview

一个按钮和datagridview。如果按钮text1(“编辑数据库”)datagridview读入数据库,如果按钮text2(“ACCEPT CHANGES”)datagridview写入数据库,但后者不能发生,无论我做什么。没有错误只是不会更改数据库文件。

            Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\PD_Z.mdb"
        Dim SQLString As String = "SELECT * FROM ZARADE"
        Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
        Dim DataSet1 As New DataSet()
        Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)
        OleDBConn1.Open()
        OleDbDataAdapter1.Fill(DataSet1, "ZARADE")
        DataGridView1.DataSource = DataSet1.Tables("ZARADE")
        DataGridView1.Columns.Remove(DataGridView1.Columns(0).Name)

        If Button2.Text = "Edit database" Then
            DataGridView1.ReadOnly = False
            Button2.Text = "ACCEPT CHANGES"
            Button2.ForeColor = Color.DarkRed
            Button1.Enabled = False
            ComboBox1.Enabled = False

        Else
    Button2.Text = "Edit database"
            DataGridView1.ReadOnly = True
            Button2.ForeColor = Color.Black
            Button1.Enabled = True
            ComboBox1.Enabled = True

            'Dim cb As New OleDbCommandBuilder(OleDbDataAdapter1) ' THIS ONE DOESN'T WORK
            '  cb.QuotePrefix = "["
            '  cb.QuoteSuffix = "]"
            '  OleDbDataAdapter1.Update(DataSet1.Tables("ZARADE"))

            Using con = New OleDbConnection(ConnString) ' THIS ONE DOESN'T WORK TOO
                Me.Validate()
                OleDbDataAdapter1.Update(DataSet1.Tables("ZARADE"))
                DataSet1.AcceptChanges()
            End Using
            DataSet1.AcceptChanges()
            OleDBConn1.Close()

此代码位于Button_click事件中。

1 个答案:

答案 0 :(得分:0)

执行后

OleDbDataAdapter1.Fill(DataSet1, "ZARADE")

数据集中的所有行都有RowWtate = Unchanged ...所以当你调用Update(DataSet1 ....)时没有什么可以更新...检查返回值...我猜它会是0因为没有行已受到影响......