且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Excel文件导入SQL数据库

更新时间:2022-10-17 23:10:43

an>
Dim cmd As OleDbCommand = OleDbCommand(query_excel,koneksi_excel)
Dim rd As OleDbDataReader
rd = cmd.ExecuteReader()

Dim koneksi As SqlConnection()
Dim koneksidatabase As String = server = DESKTOP-KJQ8PNO \ SVQPRESS; DATABASE = otto; Integrated Security = True
koneksi.ConnectionString = koneksidatabase
koneksi.Open()

使用 bulkcopy 作为 SqlBulkCopy = SqlBulkCopy(koneksi)
bulkcopy.DestinationTableName = DAFTAR1
bulkcopy.BulkCopyTimeout = 600
bulkcopy.WriteToServer(rd)
rd.Close()
koneksi.Close ()

MsgBox( 数据上传到数据库,MsgBoxStyle.Information , 已上传
TextBox1.Text =
结束 使用

结束 Sub







祝你好运



Nick


这是第2部分的代码o如果我的评论被调整为执行导入单个文件的工作,您需要做的就是编写收集文件名的子文件并依次为每个文件调用下面的子文件;



 公共  Sub  ImportExcelFielToTable(ExcelFile 作为 字符串,TargetTable 作为  String 
Dim koneksi_excel As System.Data.OleDb.OleDbConnection( Provider = Microsoft.ACE.OLEDB。 12.0;数据来源='& ExcelFile& ';扩展属性=Excel 12.0 Xml; HDR = YES
koneksi_excel.Open()
Dim query_excel As 字符串 = 从[Sheet1


中选择*
Dim cmd 作为 OleDbCommand = OleDbCommand(query_excel,koneksi_excel)
Dim rd As OleDbDataReader
rd = cmd.ExecuteReader()

使用 koneksi 作为 SqlConnection( server = DESKTOP-KJQ8PNO\SQLEXPRESS ; DATABASE = otto; Integrated Security = True
koneksi.Open()

使用 bulkcopy As SqlBulkCopy = SqlBulkCopy(koneksi)
bulkcopy.DestinationTableName = TargetTable
bulkcopy.BulkCopyTimeout = 600
bulkcopy.WriteToServer(rd)
rd.Close()

MsgBox( 数据上传到数据库,MsgBoxStyle.Information, 已上传)
TextBox1.Text =
结束 使用
koneksi.Close()
结束 使用
结束 Sub


hello guys
so I had this problem with a simple project that I build, I wanted to import more than one excel files into tables in the database.
I could only import one file to the table, so I want to import multiple excel files to multiple tables in the database. is anyone can help me ? I would really appreciate it

What I have tried:

here's the code that I use and it only works to import one file to one table, and what I need is to import more than one files into different tables in the database.


Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & TextBox1.Text & "';Extended Properties=""Excel 12.0 Xml;HDR=YES""")
       koneksi_excel.Open()
       Dim query_excel As String = "Select * from [Sheet1$]"
       Dim cmd As OleDbCommand = New OleDbCommand(query_excel, koneksi_excel)
       Dim rd As OleDbDataReader
       rd = cmd.ExecuteReader()

       Dim koneksi As New SqlConnection()
       Dim koneksidatabase As String = "server=DESKTOP-KJQ8PNO\SQLEXPRESS;DATABASE=otto;Integrated Security=True"
       koneksi.ConnectionString = koneksidatabase
       koneksi.Open()

       Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(koneksi)
           bulkcopy.DestinationTableName = "DAFTAR1"
           bulkcopy.BulkCopyTimeout = 600
           bulkcopy.WriteToServer(rd)
           rd.Close()
           koneksi.Close()

           MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded")
           TextBox1.Text = ""
       End Using

   End Sub




Best regards

Nick

" Dim cmd As OleDbCommand = New OleDbCommand(query_excel, koneksi_excel) Dim rd As OleDbDataReader rd = cmd.ExecuteReader() Dim koneksi As New SqlConnection() Dim koneksidatabase As String = "server=DESKTOP-KJQ8PNO\SQLEXPRESS;DATABASE=otto;Integrated Security=True" koneksi.ConnectionString = koneksidatabase koneksi.Open() Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(koneksi) bulkcopy.DestinationTableName = "DAFTAR1" bulkcopy.BulkCopyTimeout = 600 bulkcopy.WriteToServer(rd) rd.Close() koneksi.Close() MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded") TextBox1.Text = "" End Using End Sub




Best regards

Nick


Here is your code for part 2 of my comments adjusted to do the job of importing a single file, all you need to do is write the sub that collects the file names and call the sub below for each file in turn;

Public Sub ImportExcelFielToTable(ExcelFile As String, TargetTable As String)
Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ExcelFile & "';Extended Properties=""Excel 12.0 Xml;HDR=YES""")
        koneksi_excel.Open()
        Dim query_excel As String = "Select * from [Sheet1


" Dim cmd As OleDbCommand = New OleDbCommand(query_excel, koneksi_excel) Dim rd As OleDbDataReader rd = cmd.ExecuteReader() Using koneksi As New SqlConnection("server=DESKTOP-KJQ8PNO\SQLEXPRESS;DATABASE=otto;Integrated Security=True") koneksi.Open() Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(koneksi) bulkcopy.DestinationTableName = TargetTable bulkcopy.BulkCopyTimeout = 600 bulkcopy.WriteToServer(rd) rd.Close() MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded") TextBox1.Text = "" End Using koneksi.Close() End Using End Sub