代码vb.net从访问sdf导入数据时出错

问题描述 投票:0回答:1

[enter image description here我正在vb.net中使用SQL Server Compact 3.5数据库文件(.sdf)

解析查询时出错。 [令牌行编号,令牌行偏移量,令牌错误,]

Dim flag As Boolean
    Dim flag2 As Boolean
    Me.OpenFileDialog1.Filter = "mdb files (*.mdb)|"
    Me.OpenFileDialog1.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*"
    flag2 = (Me.OpenFileDialog1.ShowDialog() = DialogResult.OK)
    If flag2 Then
        flag = (Operators.CompareString(FileSystem.Dir(Me.OpenFileDialog1.FileName, FileAttribute.Normal), "", False) <> 0)
    End If
    Dim myConnectionStringMDB As String = "provider=Microsoft.Ace.OLEDB.12.0;" & "data source=" & Me.OpenFileDialog1.FileName
    Dim myConnectionStringSQL As String = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;" & "Data Source=" & Application.StartupPath & "\Archive.sdf"
    Using conSQL As OleDbConnection = New OleDbConnection(), conMDB As OleDbConnection = New OleDbConnection()
        conSQL.ConnectionString = myConnectionStringSQL
        conSQL.Open()
        conMDB.ConnectionString = myConnectionStringMDB
        conMDB.Open()
        Using cmdSQL As OleDbCommand = New OleDbCommand(), cmdMDB As OleDbCommand = New OleDbCommand()
            cmdMDB.CommandType = Data.CommandType.Text
            cmdMDB.Connection = conMDB
            cmdMDB.CommandText = "SELECT * FROM [student]"
            Dim daMDB = New System.Data.OleDb.OleDbDataAdapter(cmdMDB)
            Dim dt = New Data.DataTable()
            daMDB.Fill(dt)
            For Each dr As Data.DataRow In dt.Rows
                ' change row status from "Unchanged" to "Added" so .Update below will insert them
                dr.SetAdded()
            Next
            cmdSQL.CommandType = Data.CommandType.Text
            cmdSQL.Connection = conSQL
            cmdSQL.CommandText = "SELECT * FROM [student]"
            Dim daSQL = New System.Data.OleDb.OleDbDataAdapter(cmdSQL)
            Dim cbuilderMDB = New OleDbCommandBuilder(daSQL)
            cbuilderMDB.QuotePrefix = "["
            cbuilderMDB.QuoteSuffix = "]"
            daSQL.Update(dt)
        End Using
        conSQL.Close()
        conMDB.Close()
    End Using
database vb.net sqlcommand sdf
1个回答
0
投票

我摆脱了多余的布尔变量flagflag2,只是直接测试了这些值。

我将Using块划分为2个块​​,因此可以在下一组对象开始工作之前关闭并放置第一组对象。

我通过尽可能将属性直接传递给对象的构造函数来缩短代码。 DataAdapterStringBuilder也公开了.Dispose方法,因此我将它们包括在Using块中。

daMDB.AcceptChangesDuringFill = False

此代码行允许.DataRowState保留为Added(通常通过Unchanged方法将其更改为.Fill),因此DataTable将准备好将所有记录添加到第二个表没有循环。

我不相信学生在两个数据库中都是保留字,所以我删除了方括号。

如果.Fill.UpdateDataAdapter方法都发现连接已关闭,则它们将.Open.Close都连接。如果他们发现它是打开的,则将其保持打开状态。因此,不必关闭连接。 End Using还将关闭并放置该块的“使用”部分(包括逗号的第一行)中包含的所有对象。

Private Sub OPCode()
    OpenFileDialog1.Filter = "mdb files (*.mdb)|*.mdb|All files (*.*)|*.*"
    Dim MDBFile As String
    If OpenFileDialog1.ShowDialog = DialogResult.OK AndAlso Not String.IsNullOrEmpty(OpenFileDialog1.FileName) Then
        MDBFile = OpenFileDialog1.FileName
    Else
        Exit Sub
    End If
    Dim myConnectionStringMDB As String = "provider=Microsoft.Ace.OLEDB.12.0;" & "data source=" & MDBFile
    Dim myConnectionStringSQL As String = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;" & "Data Source=" & Application.StartupPath & "\Archive.sdf"
    Dim dt = New Data.DataTable()
    Using conMDB As OleDbConnection = New OleDbConnection(myConnectionStringMDB),
            cmdMDB As OleDbCommand = New OleDbCommand("SELECT * FROM student", conMDB),
            daMDB As New System.Data.OleDb.OleDbDataAdapter(cmdMDB)
        daMDB.AcceptChangesDuringFill = False
        daMDB.Fill(dt)
    End Using
    Using conSQL As OleDbConnection = New OleDbConnection(myConnectionStringSQL),
                cmdSQL As OleDbCommand = New OleDbCommand("SELECT * FROM student", conSQL),
                daSql As New OleDbDataAdapter(cmdSQL),
                cbuilderMDB As New OleDbCommandBuilder(daSql)
        daSql.Update(dt)
    End Using
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.