如何将Excel文件导入SQL数据库 [英] How can I import excel files to SQL database

查看:122
本文介绍了如何将Excel文件导入SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好家伙

所以我有一个我构建的简单项目的问题,我想将多个excel文件导入数据库中的表。

我只能将一个文件导入到表中,所以我想将多个excel文件导入到数据库中的多个表中。有人可以帮帮我吗?我真的很感激



我尝试过:



这里是我使用的代码只能将一个文件导入到一个表中,我需要的是将多个文件导入到数据库中的不同表中。





  Dim  koneksi_excel  As   System.Data.OleDb.OleDbConnection(  Provider = Microsoft。 ACE.OLEDB.12.0;数据源='& TextBox1.Text&  ';扩展Properties =Excel 12.0 Xml; HDR = YES
koneksi_excel.Open()
Dim query_excel 作为 字符串 = 从[Sheet1 $]中选择*
Dim cmd 作为 OleDbCommand = OleDbCommand(query_excel,koneksi_excel)
Dim rd As OleDbDataReader
rd = cmd.ExecuteReader()

Dim koneksi As SqlConnection()
Dim koneksidatabase 作为 字符串 = 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







祝你好运



尼克

解决方案


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

Dim koneksi As SqlConnection()
Dim koneksidatabase As < span class =code-keyword> 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, < span class =code-string>已上传)
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


这篇关于如何将Excel文件导入SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆