0

在此处输入图像描述我在 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
4

1 回答 1

0

我摆脱了额外的布尔变量flagflag2直接测试了这些值。

我将这些Using块分成 2 个块,以便第一组对象可以在下一组开始工作之前关闭和处置。

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

daMDB.AcceptChangesDuringFill = False

这行代码允许.DataRowState保持原样Added(通常Unchanged.Fill方法更改为),因此,DataTable将准备好将所有记录添加到第二个表中而无需循环。

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

如果发现连接已关闭,则将和连接的the.Fill.Update方法。如果他们发现它打开,他们就会让它保持打开状态。因此,没有必要关闭连接。此外,关闭并处理块的 Using 部分(包括逗号的第一行)中包含的所有对象。DataAdapter.Open.CloseEnd 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
于 2020-03-01T01:10:10.960 回答