简体   繁体   English

从数据表获取行值

[英]getting row value from datatable

Dim flag As Boolean = True Dim标志为Boolean = True

Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) 昏暗的FileName作为字符串= Path.GetFileName(FileUpload1.PostedFile.FileName)

Dim fileExtension As String = Path.GetExtension(FileUpload1.PostedFile.FileName) 昏暗的fileExtension为字符串= Path.GetExtension(FileUpload1.PostedFile.FileName)

Dim fileLocation As String = Server.MapPath("~/Upload/" & fileName) 昏暗的fileLocation为字符串= Server.MapPath(“〜/ Upload /”&fileName)

       'Check whether file extension is xls or xslx
        If fileExtension = ".xls" Then
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileLocation & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
        ElseIf fileExtension = ".xlsx" Then
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileLocation & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
        End If

        'Create OleDB Connection and OleDb Command

        Dim con As New OleDbConnection(connectionString)
        Dim cmd As New OleDbCommand()
        cmd.CommandType = System.Data.CommandType.Text
        cmd.Connection = con
        Dim dAdapter As New OleDbDataAdapter(cmd)
        Dim dtExcelRecords As New DataTable()
        con.Open()
        Dim dtExcelSheetName As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
        Dim getExcelSheetName As String = dtExcelSheetName.Rows(0)("Table_Name").ToString()
        cmd.CommandText = "SELECT * FROM [" & getExcelSheetName & "]"
        dAdapter.SelectCommand = cmd
        dAdapter.Fill(dtExcelRecords)
        con.Close()

        Dim row As DataRow
        For Each row In dtExcelRecords.Rows
            If Include.ConvertDbNullToEmpyString(row(2)) = "" Then
                MsgBox("Error in saving!")
                Exit For

            End If
            MsgBox(row(0) & " " & row(1) & " " & row(2) & " " & row(3))
        Next


        GridView1.DataSource = dtExcelRecords

        GridView1.DataBind()
        ViewState("file") = dtExcelRecords


        Dim dataSet As DataSet = New DataSet("dataSet")
        dataSet.Tables.Add(dtExcelRecords)
        '' Display the DataSet contents as XML.
        Console.WriteLine(dataSet.Tables(0).DataSet.GetXml())

I had code which use to upload file excel into datatable and show at gridview. 我有用于将文件excel上载到数据表并在gridview中显示的代码。

After show at gridview, the excel value which has been convert into datatable and showed at gridview will be saved to database. 在gridview中显示后,已转换为datatable并在gridview中显示的excel值将保存到数据库中。

My problem : 我的问题 :

 Dim row As DataRow For Each row In dtExcelRecords.Rows If Include.ConvertDbNullToEmpyString(row(2)) = "" Then MsgBox("Error in saving!") Exit For End If MsgBox(row(0) & " " & row(1) & " " & row(2) & " " & row(3)) Next 

that was code to get the row value. 那是获取行值的代码。

But how to set datatable with new data row? 但是如何设置带有新数据行的数据表呢?

If i had null value in row 2 i wanted proses input data to datatable will stoped! 如果我在第2行中具有空值,则我希望将proses输入到datatable的数据停止! And showing with new value from datarow value into gridView 并将新值从数据行值显示到gridView中

thanks 谢谢

-------------------------------------- EDIT ---------------------------------- --------------------------------------编辑----------- -----------------------

example : 例如:

upload file excel : 上传文件excel:

col1 ---- col2 ---- col3 ---- col4 col1 ---- col2 ---- col3 ---- col4

tes1 ---- test1 ---- test1 ---- test1 tes1 ---- test1 ---- test1 ---- test1

tes2 ---- test2 ---- ---- test2 tes2 ---- test2 ---- ---- test2

tes3 ---- test3 ---- test3 ---- test3 tes3 ---- test3 ---- test3 ---- test3

tes4 ---- test4 ---- ---- test4 tes4 ---- test4 ---- ---- test4

and the result at gridview must like this : 并且gridview的结果必须是这样的:

col1 ---- col2 ---- col3 ---- col4 col1 ---- col2 ---- col3 ---- col4

tes1 ---- test1 ---- test1 ---- test1 tes1 ---- test1 ---- test1 ---- test1

tes3 ---- test3 ---- test3 ---- test3 tes3 ---- test3 ---- test3 ---- test3

如果将Exit For更改为Exit Sub,则此时将停止处理

Try, 尝试,

                Dim dtData As DataTable
                dtData = New DataTable
                dtData.Columns.Add("Row0")
                dtData.Columns.Add("Row1")
                dtData.Columns.Add("Row2")
                dtData.Columns.Add("Row3")

                Dim row As DataRow
                For Each row In dtExcelRecords.Rows
                    If Include.ConvertDbNullToEmpyString(row(2)) = "" Then
                        MsgBox("Error in saving!")
                        Exit For 'or exit Sub
                    End If
                    Dim dr As DataRow = dtData.NewRow

                    dr("Row0") = row(0)
                    dr("Row1") = row(1)
                    dr("Row2") = row(2)
                    dr("Row3") = row(3)

                    dtData.Rows.Add(dr) 'Add this line

                Next

                'Bind dtData to gridview
                'Save data in dtData to database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM