To import data from excel to DB

php中文网
发布: 2016-06-07 15:49:00
原创
1656人浏览过

Here,I introducesome methods to import data from excel to DB, 1 By OPENDATASOURCE SELECT * FROM OPENDATASOURCE ( 'Microsoft.Jet.OLEDB.4.0' , 'Excel 8.0;DataBase=D:/TEST.xls' )...[sheet1$] Note: Sometimes, error message will occor when exec

Here,I introduce some methods to import data from excel to DB,

1  By OPENDATASOURCE

SELECT
* FROM OPENDATASOURCE(
    'Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;DataBase=D:/TEST.xls')...[sheet1$]

Note: Sometimes, error message will occor when executing above script like this:
Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

The solution issetting Ad Hoc Distributed Queries to 1,you can refer to my post: http://www.cnblogs.com/Burgess/archive/2008/09/24/1298195.html


2   By Linked server

EXEC sp_addlinkedserver  --add linked server
    @server = N'MyExcel',
    @srvproduct = N'Jet 4.0',
    @provider = N'Microsoft.Jet.OLEDB.4.0',
    @datasrc = N'd:/TEST.xls',
    @provstr = N'Excel 8.0'
GO

Note:You can also add linked server by below method:

To import data from excel to DB

exec sp_addlinkedsrvlogin 'MyExcel','false' --login without account (Optional)
go

FaceSwapper
FaceSwapper

FaceSwapper是一款AI在线换脸工具,可以让用户在照片和视频中无缝交换面孔。

FaceSwapper 960
查看详情 FaceSwapper

select * from MyExcel...sheet1$  --query data
go

 

 3  By VBA

 

To import data from excel to DB To import data from excel to DB Code
 1To import data from excel to DB To import data from excel to DB Private Sub cmdInsert_Click()Sub cmdInsert_Click()
 2To import data from excel to DB
 3To import data from excel to DB     Dim LinCnt As Integer
 4To import data from excel to DB     LinCnt = 6
 5To import data from excel to DB     If InputBox("Please input password1""口令输入框", , 87004700= "password" Then
 6To import data from excel to DB         Rows("6:6").Select
 7To import data from excel to DB         Rows("6:65536").Select
 8To import data from excel to DB         Range("A6").Select
 9To import data from excel to DB         
10To import data from excel to DB         Connection.Open " Provider=SQLOLEDB.1;Persist Security Info=True;User ID=User_id;Password=PWD; Initial Catalog=DB_name;Data Source=Server_ip;Connect Timeout=60 "
11To import data from excel to DB         Connection.CursorLocation = adUseClient
12To import data from excel to DB         CMD.ActiveConnection = Connection
13To import data from excel to DB
14To import data from excel to DB         Do While Cells(LinCnt, "C" ""
15To import data from excel to DB             If txtSoldto.Text = "" And txtQcimat.Text = "" And Cells(LinCnt, "G"= "" Then
16To import data from excel to DB                 MsgBox "Please maintain To import data from excel to DB "
17To import data from excel to DB                 Exit Sub
18To import data from excel to DB             Else
19To import data from excel to DB                 SQLstmt = " insert into table_name values('" & VBA.Trim(txt1.Text) & "','" & VBA.Trim(txt2.Text) & "','" & Cells(LinCnt, "C"& "','" & Cells(LinCnt, "D"& "','" & Cells(LinCnt, "E"& "','" & Cells(LinCnt, "F"& "','" & Cells(LinCnt, "G"& "','" & Cells(LinCnt, "H"& "','" & Cells(LinCnt, "I"& "','" & Cells(LinCnt, "J"& "') "
20To import data from excel to DB                 CMD.CommandText = SQLstmt
21To import data from excel to DB                 CMD.Execute
22To import data from excel to DB                 LinCnt = LinCnt + 1
23To import data from excel to DB             End If
24To import data from excel to DB         Loop
25To import data from excel to DB         Connection.Close
26To import data from excel to DB         Exit Sub
27To import data from excel to DB     Else
28To import data from excel to DB         MsgBox "You have no right to insert!"
29To import data from excel to DB         Exit Sub
30To import data from excel to DB     End If
31To import data from excel to DB End Sub


 4   By SQL Server Import and Export Wizard
Detailed oprating steps is abbreviated here.

5  Other methods:
Please refer to http://support.microsoft.com/default.aspx/kb/321686

相关标签:
WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号