我需要从我的Excel中读取行并将其插入SQl服务器。在插入时将检查这些行是否存在,根据标志ll b返回到前端。
到目前为止,我已经进行过专栏阅读。请任何人帮我从excel中读取数据行,并在VB.Net中插入SQL服务器
需要使用数据表和数据集。 在VB.NET中
Public Class Form1
'#Region "Decleration"
'Dim exApp As Microsoft.Office.Interop.Excel.Application
'Dim exSheet As Microsoft.Office.Interop.Excel.Worksheet
'Dim exCell As Microsoft.Office.Interop.Excel.Range
'Dim dataset1 As New DataSet
'Dim rCount As Integer = 0
'Dim cCount As Integer = 0
'Dim objValues As Object
'Dim totalRecords As Integer = 0
'#End Region
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objWorkSheets As Excel.Worksheet
Dim ExcelSheetName As String = ""
' objExcel = CreateObject("Excel.Application")
' objWorkBook = objExcel.Workbooks.Open("C:\Users\Jidh\Desktop\tttttttttttttttttt.xlsx")
End Sub
Private Sub OpenFileDialog_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles FileUpload1.FileOk
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub btnChoseDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnChoseDoc.Click
If FileUpload1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Dim DS As DataSet
Dim MyCommand As OleDb.OleDbDataAdapter
Dim MyConnection As OleDb.OleDbConnection
Dim dtExcelSchema As DataTable
MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jidh\Desktop\tttttttttttttttttt.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")
MyConnection.Open()
dtExcelSchema = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
MyConnection.Close()
MyConnection.Open()
' Select the data from Sheet1 of the workbook.
MyCommand = New OleDb.OleDbDataAdapter("Select * from [" & SheetName & "]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
grvRecords.DataSource = DS.Tables(0).DefaultView
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try
End If
End Sub
'Private Sub ReleaseComObject(ByVal obj As Object)
' Try
' System.Runtime.InteropServices.Marshal.ReleaseComObject(objValues)
' obj = Nothing
' Catch ex As Exception
' obj = Nothing
' Finally
' GC.Collect()
' End Try
'End Sub
结束班
答案 0 :(得分:1)
所以我编译了你的代码,它似乎很好地从Excel中提取了所有数据。当然,如果您不希望第一行数据成为列标题,那么您只需将HDR更改为NO即可解决该问题。接下来,您已经要求将其放入您的代码已经执行的数据集中。然后可以很容易地将其转移到数据表中,然后就可以了。
现在,您要求将它放入SQL的部分。有几种不同的方式。您可以将所有数据放入XML文档,然后读取SQL存储过程并将其插入。接下来,您可以循环遍历数据表中的每一行并以此方式添加它。我已经选择了我认为最好的方法,基于MSDN(MSDN - Bulk Copy Data into SQL)。
让我知道你怎么走。
Public YourDatatableWithExcelData As New System.Data.DataTable
Private Sub btnChoseDoc_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If FileUpload1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Dim DS As DataSet
Dim MyCommand As OleDb.OleDbDataAdapter
Dim MyConnection As OleDb.OleDbConnection
Dim dtExcelSchema As System.Data.DataTable
MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dawid\Desktop\Book1.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")
MyConnection.Open()
dtExcelSchema = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
MyConnection.Close()
MyConnection.Open()
' Select the data from Sheet1 of the workbook.
MyCommand = New OleDb.OleDbDataAdapter("Select * from [" & SheetName & "]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
grvRecords.DataSource = DS.Tables(0).DefaultView
YourDatatableWithExcelData = DS.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try
End If
End Sub
Private Sub InsertIntoSQL(sender As Object, e As EventArgs) Handles Button2.Click
Using destinationConnection As SqlConnection = New SqlConnection("Data Source=servername;" & "Initial Catalog=databasename;" & "User ID=username;" & "Password=userpassword;")
destinationConnection.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(destinationConnection)
bulkCopy.DestinationTableName = "dbo.YourTableNameToCopyDataTo"
Try
bulkCopy.WriteToServer(YourDatatableWithExcelData)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
End Using
End Sub
我们创建了一个名为YourDatatableWithExcelData
的全局声明数据表,它被赋予数据集的值,然后我们使用Button2.Click调用InsertIntoSQL函数中的数据表。
希望这有帮助。