我加载数据表两次&使用布尔值更新行项

时间:2018-05-14 15:47:05

标签: vb.net datatable sql-update spreadsheet

我在模块中调用以下函数

Public Function GetExcelData(ByVal ExcelFile As String)As System.Data.DataTable

然后我有以下代码

            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            gblCompName = openFileDialog1.FileName
        End If

        Dim reader As New DataTableReader(GetExcelData(gblCompName))
        Dim table As New DataTable

        table.Load(reader)
        table.Columns.Add("Single", GetType(Boolean), False)
        table.Columns.Add("CouplesInFinal", GetType(Int32))
        table.Columns.Add("EvtNum", GetType(String))
        table.Columns.Add("EvtStruct", GetType(Int32))
        table.Columns.Add("EvtCplID", GetType(Int32))
        table.Columns.Add("CouplesInClass", GetType(Int32))
        table.Columns.Add("Valid", GetType(Boolean), True)

        Dim result() As DataRow = table.Select("[class]" Like "Single")
        For Each row In result
            If row.Item("Class") Like "Single" Then
                table.Rows(0)("Single") = True
            End If
        Next

        DataGridView1.DataSource = table

我的逻辑告诉我,我将表加载两次&数据行字段" Single"是布尔值,如果字符串字段" class"我试图更新为True就像" Single"

我在一个人中意识到它有两个问题,但似乎加载速度相当缓慢。它是所有形式的程序。对此有任何建议非常欢迎,谢谢

2 个答案:

答案 0 :(得分:0)

Dim reader As New DataTableReader(GetExcelData(gblCompName))
Dim table As New DataTable

如果没有看到GetExcelData,很难说你是否填两次。如果此函数返回填充的DataTable,则

Dim table as DataTable = GetExcelData(gblCompName)

并删除table.Load和DataReader。 这是我针对示例数据库更新DataTable的版本。

Private Sub TestDataTableUpdate()
        Try
            Using cmd As New SqlCommand("Select * From Coffees", CoffeeCn)
                Dim dt As New DataTable
                CoffeeCn.Open()
                Using dr As SqlDataReader = cmd.ExecuteReader
                    dt.Load(dr)
                End Using
                CoffeeCn.Close()
                For index As Integer = 0 To dt.Rows.Count - 1
                    If dt.Rows(index)("Name").ToString = "Cinnamon Stick" Then
                        dt.Rows(index)("Roast") = "N/A"
                    End If
                Next
                'The above does not update the database
                Dim da As New SqlDataAdapter(cmd) 'provides the connection and Select command
                Dim sqlCB As New SqlCommandBuilder(da) 'Associates the DataAdapter to the command builder
                sqlCB.GetUpdateCommand() 'Retrieve the update command for the DataAdapter
                'Note: the DataAdapter opens and closes the connection for you
                da.Update(dt) 'This updates the database by finding the changed rows in the datatable
                'and running the Update command
                dt.AcceptChanges() 'Reset the row status to unchanged
                DataGridView1.DataSource = dt
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            CoffeeCn.Close()
        End Try
    End Sub

PS。我也是自学成才。

答案 1 :(得分:0)

我开始工作的表更新方法是

scanf()

我在这个行表上取出了“,False”.Columns.Add(“Single”,GetType(Boolean)