开发者

VB.NET 2008 DataGridView not Updating Visual Foxpro Database

I'm using VB.NET开发者_如何学Python 2008 with a DataGridView and I'm interfacing to a Visual Foxpro 6 database using the vfpoledb.1 driver. When I change a value in the description field, it changes in the DataGridView but the database never gets updated. Do I need to use code to force the changes to take place?

Here's the code I'm using:

Imports System.Data.OleDb

Public Class Form1
    Dim sConString As String = "Provider=vfpoledb.1;Data Source=C:\MyDatabase.dbc;Mode=3;"
    Dim con As OleDbConnection = New OleDbConnection(sConString)

    Private Function FetchData()

        con.Open()
        Dim ds As DataSet = New DataSet()
        Dim sSQL As String
        sSQL = "SELECT item_cd, item_desc FROM invent;"

        Dim cmd As OleDbCommand = New OleDbCommand(sSQL, con)
        Dim daInv As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim iRecCount As Integer
        iRecCount = daInv.Fill(ds, "invent")
        Me.DataGridView1.DataSource = ds.Tables("invent").DefaultView
    End Function

    Private Sub btnFetchData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFetchData.Click
        Call FetchData()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        con.Close()
        con = Nothing
    End Sub
End Class


Did this code will update the Table and push the changes back to Database? I don't think so. I tried the same code with SQL Server 2005, and it is not working(I don't have a FoxPro db). So I modified the code and now it is working fine.

Imports System.Data.OleDb

Public Class Form1
    Dim sConString As String = "Provider=vfpoledb.1;Data Source=C:\MyDatabase.dbc;Mode=3;"
    Dim con As OleDbConnection = New OleDbConnection(sConString)
    Dim daInv As OleDbDataAdapter
    Dim ds As DataSet = New DataSet()
    Private Sub FetchData()
        con.Open()
        Dim sSQL As String
        sSQL = "SELECT item_cd, item_desc FROM invent;"
        Dim cmd As OleDbCommand = New OleDbCommand(sSQL, con)
        daInv = New OleDbDataAdapter(cmd)
        Dim builder As New OleDbCommandBuilder(daInv)
        Dim iRecCount As Integer
        iRecCount = daInv.Fill(ds, "invent")
        Me.DataGridView1.DataSource = ds.Tables("invent").DefaultView
    End Sub

    Private Sub btnFetchData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFetchData.Click
        Call FetchData()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        daInv.Update(ds.Tables("invent"))
        con.Close()
        con = Nothing
    End Sub
End Class

Hope it works :)


I found the underyling problem and resolved it. The VFP table I'm accessing does not have a primary key defined. It does have indexes but they are simply marked as "Regular" indexes (using VFP terminology).

I was able to use a VFP tool call vRunFox (Visual Run Fox) from Ed Leafe's website to modify the table structure. I had to use some VFP commands to bring up the Table Designer window.

I also had to add some code to my FormClosing event. Here's what my FormClosing event looks like now:

    Dim myBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(daInv)
    myBuilder.GetUpdateCommand()
    daInv.UpdateCommand = myBuilder.GetUpdateCommand
    daInv.Update(ds.Tables("invent"))
    con.Close()
    con = Nothing
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜