【问题标题】:Why doesn't my db update the merged tables?为什么我的数据库不更新合并表?
【发布时间】:2018-05-09 20:37:35
【问题描述】:

我一直在试图弄清楚为什么我的数据库没有从下面的代码中接收到合并的数据。

以下代码的作用如下(PS:它不干净,但它被注释了!):

  1. 连接到选定的 XLSX 工作簿(现在一直在寻找 Sheet1)。
  2. 将 Sheet1 加载到表名为“XLSheet”的数据集 (DtSet) 中。
  3. 我创建了一个名为 dt 的 DataTable 变量并将其指向上表“XLSheet”
  4. 我连接到我的应用程序本地 SQLite DB。
  5. 我再次在 SQLite DB 中执行命令文本到 DROP TABLE XLImport(我尝试导入的表)。
  6. 我使用动态 SQL 重新创建表以将新的 CREATE TABLE 语句连接在一起,因为每个 XLSheet 都将具有唯一的列标题(它们来自很多很多来源)。 这可确保我的 XLSheet 和 XLImport 表具有完全相同的架构,并且都没有 PK
  7. 我从指向新 XLImport 表的 SQLite 数据适配器创建了一个数据集。
  8. 我创建了一个 DataTable 对象并将其指向 XLImport 表。
  9. 我使用 dt2.Merge(dt) 合并两个数据表 10.???如何通过 dataAdapter 将该合并提交回 SQLite 数据库??? 这是代码

    Private Sub cmdImportFile_Click(sender As Object, e As EventArgs) Handles cmdImportFile.Click
    Dim bHeaders As Boolean = chkHeaders.CheckState
    Dim oFile As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(txtFilePath.Text)
    Dim sFile As String = oFile.Name
    Dim sFilePath As String = oFile.DirectoryName
    Dim da As SQLite.SQLiteDataAdapter
    Dim ds As New DataSet
    Dim dt2 As New DataTable
    
    Try
        Dim XLConn As System.Data.OleDb.OleDbConnection
        Dim DtSet As New System.Data.DataSet
        Dim XLDataAdapter As System.Data.OleDb.OleDbDataAdapter
    
    
        'Create connection to selected XLSX workbook, Sheet1
        XLConn = New System.Data.OleDb.OleDbConnection _
        ("provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & oFile.FullName & "; Extended Properties=""Excel 12.0 Xml;HDR=" & bHeaders & "; IMEX=1"";")
        XLDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", XLConn)
    
        'Open and fill dataset to table 'XLSheet'
        XLDataAdapter.Fill(DtSet, "XLSheet")
        XLConn.Close()
    
        'Setup DataTable object
        Dim dt As DataTable = DtSet.Tables("XLSheet")
    
        'Connect to SQLite
        Dim sAppPath As String = Application.StartupPath
    
        'When debugging, this changes the AppPath value to remove the \bin\Debug portion of the AppPath
        If Debugger.IsAttached Then sAppPath = sAppPath.Replace("\bin\Debug", "")
        Dim connString As String = "Data Source = " & sAppPath & "\biodata.db;version=3;"
        Dim sDBPath As String = sAppPath & "\biodata.db"
    
        'Connect to DB
        conn = New SQLiteConnection(connString)
        conn.Open()
    
        'Drop Existing "XLImport" table
        Dim cmd As SQLiteCommand
        Dim sDROPSQL As String = "DROP TABLE IF EXISTS XLImport;"
        cmd = conn.CreateCommand()
        cmd.CommandText = sDROPSQL
        cmd.ExecuteReader()
        cmd.Dispose()
    
        'Build dynamic SQL string for CREATE TABLE based on imported XLS file (they have dynamic headers!)
        Dim iMax As Integer = dt.Columns.Count
        Dim i As Integer = 1
        Dim sSQL As String = "CREATE TABLE XLImport("
    
        For Each c As DataColumn In dt.Columns
            Debug.Print(c.DataType.ToString)
            sSQL = sSQL & "[" & c.ColumnName & "] "     & ConvertXLDataTypeToSQLite(c.DataType.ToString)
            If i < iMax Then sSQL = sSQL & ","
            i = i + 1
        Next
    
        sSQL = sSQL & ");"
    
    
        'Create Table "XLImports" from Dynamic SQL Statement
        cmd = conn.CreateCommand
        cmd.CommandText = sSQL
        cmd.ExecuteReader()
        cmd.Dispose()
        conn.Close()
    
        'Open SQLite DB 
        cmd = conn.CreateCommand
        cmd.CommandText = "SELECT * FROM XLImport"
        conn.Open()
        da = New SQLiteDataAdapter(cmd.CommandText, conn)
    
        'Fill Dataset table "XLImport" from db, which is always EMPTY.
        da.Fill(ds, "XLImport")
    
        'Create DataTable object for 'XLImport'
        dt2 = ds.Tables("XLImport")
    
        'Merge the two dataTables - NO PRIMARY KEY ON EITHER DataTable
        dt2.Merge(dt)
    
        da.Update(ds.Tables("XLImport"))
    
    
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    
    End Sub
    
    Private Function ConvertXLDataTypeToSQLite(sDataType As String) As String
    Dim sNewDataType As String
    
    Select Case sDataType
    
        Case "System.Int64"
            sNewDataType = "NUMBER"
        Case "System.Int16"
            sNewDataType = "NUMBER"
        Case "System.Byte"
            sNewDataType = "NUMBER"
        Case "System.Byte[]"
            sNewDataType = "NUMBER"
        Case "System.Int32"
            sNewDataType = "NUMBER"
        Case "System.UInt16"
            sNewDataType = "NUMBER"
        Case "System.Double"
            sNewDataType = "DOUBLE"
        Case "System.String"
            sNewDataType = "VARCHAR(255)"
        Case "System.DateTime"
            sNewDataType = "DATETIME"
        Case "System.GUID"
            sNewDataType = "INTEGER"
        Case "System.Decimal"
            sNewDataType = "DECIMAL"
        Case "System.Boolean"
            sNewDataType = "NUMBER"
        Case "System.Single"
            sNewDataType = "NUMBER"
        Case Else
            sNewDataType = "ERROR"
    End Select
    
    Return sNewDataType
    End Function
    

    结束类

任何帮助将不胜感激(以防万一,在此先感谢玛丽;))

【问题讨论】:

  • 我只看到了一些非常微不足道的东西。对于 Drop 表和创建表 cmd.ExecuteReader 应该是 cmd.ExecuteNonQuery .ExecuteReader 返回一个读取器(firehose;仅转发,只读),其中包含您不需要的数据 您已经将您的连接和命令与 @987654325 相关联@ 你已经设置了命令的属性,所以...... da = New SQLiteDataAdapter(cmd.CommandText, conn) 可以是 da = New SQLiteDataAdapter(cmd) 除了这对我来说看起来不错。有什么错误吗?插入失败?
  • 谢谢玛丽。 da.Update 触发但似乎没有抛出任何异常。我已经运行了几个测试,通过 msgbox 合并之前和之后的 dt2 的记录计数。合并前为 0。合并后为 8638(正确的记录数)。因此,记录被合并,只是没有更新回数据库。
  • 我认为问题可能是数据表没有反映任何更改。 RowState 将保持不变。你可以检查这个属性看看。
  • Mary - 我在 dt2.Rows r.SetAdded() 中添加了以下 For Each r As DataRow Next 它工作了!

标签: vb.net sqlite datatable merge


【解决方案1】:

解决了!

问题在于每个 DataRow 都有一个未更改的 RowState(正如 Mary 在 cmets 中指出的那样)。解决方案是在 Merge 和 Update 命令之间添加 For Each。

dt2.Merge(dt)

        For Each r As DataRow In dt2.Rows
            r.SetAdded()
        Next

da.Update(dt2)

为此向玛丽 +1!

【讨论】:

  • 太棒了!很高兴你得到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2020-07-25
  • 2016-12-12
  • 1970-01-01
  • 2012-05-31
相关资源
最近更新 更多