无法使用vb.net将datagrid更新为dbf

时间:2018-05-04 04:19:28

标签: vb.net datagridview dbf

我是编程新手,但很多时候没有任何课程学习和研究,所以我很多时候都遇到了问题。 Stack Over flow是解决我问题的正确位置。

这次我将datagridview更新为.dbf文件,用于使用.dbf数据库的另一个应用程序。所以请给我一个更好的解决方案来准备我的申请。

我的错误是“对于不返回任何键列信息的SelectCommand,不支持UpdateCommand的动态SQL生成。”

< 我的代码如下。

Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Dim con1 As New OleDbConnection
Dim ds1 As New DataSet
Dim dt1 As New DataTable
Dim da1 As New OleDbDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       con.ConnectionString = "Provider=vfpoledb.1;Data Source=C:\dbf_folder;Collating Sequence=machine;"
    con.Open()
    ds.Tables.Add(dt)
    da = New OleDbDataAdapter("Select * from voterlist.dbf", con)
    da.Fill(dt)
    dbfdatagrid.DataSource = dt.DefaultView
    con.Close()
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
    con1.Close()
    con1.ConnectionString = "Provider=vfpoledb.1;Data Source=C:\dbf_folder1;Collating Sequence=machine;"
    con1.Open()
    da1 = New OleDbDataAdapter("Select * from voterlist.dbf", con1)
    Dim cb = New OleDbCommandBuilder(da1)
    Dim primaryKey(1) As DataColumn
    primaryKey(1) = dt1.Columns("areaid")
    dt1.PrimaryKey = primaryKey
    da1.Fill(dt)
    dt.Merge(dt1)
    da1.Update(dt)
End Sub

1 个答案:

答案 0 :(得分:0)

您需要告诉DataSet / DataTable主键是什么:

' For dataset DS, assumed there is one table
Dim PrimaryKey(1) As DataColumn
PrimaryKey(1) = ds.Tables(0).Columns("ID")
DS.Tables(0).PrimaryKey = PrimaryKey

' For DataTable DT
Dim PrimaryKey(1) As DataColumn
PrimaryKey(1) = dt.Columns("ID")
dt.PrimaryKey = PrimaryKey

<强>更新

您使用的是正确的驱动程序,需要在DBF中创建一个Unique列。为此,请在使用之前在DBF上执行此SQL:

ALTER TABLE voterlist ALTER COLUMN areaid INT UNIQUE (I assume an integer column, if character, areaid C(somenumber) where somenumber is the size of the column)

这将在列areaid上创建一个索引,该索引将(应该)用作主键。