从数据表中获取行值
Dim flag As Boolean = True
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim fileExtension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim fileLocation As String = 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())
我有代码用于将文件 excel 上传到数据表并在 gridview 中显示。
在gridview中显示后,已转换为数据表并在gridview中显示的excel值将保存到数据库中。
我的问题:
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
这是获取行值的代码。
但是如何用新的数据行设置数据表呢?
如果我在第 2 行中有空值,我希望向数据表输入数据的过程将停止! 并将数据行值中的新值显示到 gridView 中。
- - - - - - - - - - - - - - - - - - - 编辑 - - - - - - -----------------------
示例:
上传 Excel 文件:
col1 ---- col2 ---- col3 ---- col4
tes1 ---- 测试1 ---- 测试1 ---- 测试1
tes2 ---- 测试2 ---- ---- 测试2
tes3 ---- 测试3 ---- 测试3 ---- 测试3
tes4 ---- 测试4 ---- ---- 测试4
和 gridview 的结果必须像这样:
col1 ---- col2 ---- col3 ---- col4
tes1 ---- 测试1 ---- 测试1 ---- 测试1
tes3 ---- 测试3 ---- 测试3 ---- 测试3
Dim flag As Boolean = True
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim fileExtension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim fileLocation As String = 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.
After show at gridview, the excel value which has been convert into datatable and showed at gridview will be saved to database.
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!
And showing with new value from datarow value into gridView.
-------------------------------------- EDIT ----------------------------------
example :
upload file excel :
col1 ---- col2 ---- col3 ---- col4
tes1 ---- test1 ---- test1 ---- test1
tes2 ---- test2 ---- ---- test2
tes3 ---- test3 ---- test3 ---- test3
tes4 ---- test4 ---- ---- test4
and the result at gridview must like this :
col1 ---- col2 ---- col3 ---- col4
tes1 ---- test1 ---- test1 ---- test1
tes3 ---- test3 ---- test3 ---- test3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试,
Try,
如果您将 Exit For 更改为 Exit Sub 那么您将在此时停止处理
if you change the Exit For to Exit Sub then you will stop processing at that point