简体   繁体   English

使用VB6将Excel数据导入SQL

[英]Import Excel Data into SQL using VB6

I'm completely new to Visual Basic and I now need to take a program that is entirely constructed in VB6 and add the ability to import a large set of records in bulk from an excel file into the current SQL Database. 我是Visual Basic的新手,现在我需要使用一个完全在VB6中构建的程序,并添加将大量记录从excel文件批量导入到当前SQL数据库的功能。 all of the examples I have found online are confusing and require hard-coding of the file name (ex. Using document As New Spreadsheet() document.LoadFromFile("SimpleReport.xls")) yet this needs to be called by a user anytime they get a new set of records so I need the excel file name to be specified at time of import. 我在网上找到的所有示例都令人困惑,并且需要对文件名进行硬编码(例如,将文档作为New Spreadsheet()使用document.LoadFromFile(“ SimpleReport.xls”)),但是这需要用户随时调用他们获得了一组新的记录,所以我需要在导入时指定excel文件名。

How do I import from excel to SQL using VB6? 如何使用VB6从excel导入到SQL? Can I make a variable for the excel filename or does the string value of the filename have to be hard coded? 我可以为excel文件名设置变量,还是必须对文件名的字符串值进行硬编码? If I can make a variable can/should I add set and get to it in order to specify the filename? 如果可以做一个变量,可以/应该添加集合并获取它以便指定文件名吗? Thanks 谢谢

With a 32 bit Machine (O/S): 使用32位计算机(O / S):

Dim cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\test\myfile.xls;" & _
    "Extended Properties=Excel 8.0"

'Import by using Jet Provider.
strSQL = "SELECT * INTO [odbc;Driver={SQL Server};" & _
    "Server=<server>;Database=<database>;" & _
    "UID=<user>;PWD=<password>].MyTable " & _
    "FROM [MySheet$]"
Debug.Print strSQL
cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
Debug.Print "Records affected: " & lngRecsAff

cn.Close
Set cn = Nothing

Microsoft KB : 321686 has more ideas. Microsoft KB:321686有更多想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM