Import ASP Excel Data to access, sample code in SQL Server

Source: Internet
Author: User
Import ASP Excel Data to access, sample code in SQL Server
SQL serverexcelsqlaccessasp

Import the data in Excel to access, provided that the table in access has been created.

Dim Conn
Dim conn2
Set conn = Createobject ("ADODB. Connection ")
Conn. Open "provider = Microsoft. Jet. oledb.4.0; Jet oledb: Database Password =; Data Source = c: \ book1.mdb"

Set conn2 = Createobject ("ADODB. Connection ")
Conn2.open "provider = Microsoft. Jet. oledb.4.0; Jet oledb: Database Password =; extended properties = Excel 5.0; Data Source = c: \ book1.xls"

SQL = "select * from [sheet1 $]"
Set rs = conn2.execute (SQL)
While not Rs. EOF
SQL = "insert into XXX ([a], [B], [c], [d]) values ('" & fixsql (RS (0 ))&"', '"& fixsql (RS (1) &"', '"& fixsql (RS (2) &"', '"& fixsql (RS (3 )) &"')"
Conn.exe cute (SQL)
Rs. movenext
Wend

Conn. Close
Set conn = nothing
Conn2.close
Set conn2 = nothing

Function fixsql (STR)
Dim newstr
Newstr = Str
If isnull (newstr) then
Newstr = ""
Else
Newstr = Replace (newstr ,"'","''")
End if
Fixsql = newstr
End Function

When importing data to an SQL Server database, if the Excel file is not on the same server as the database, refer to the above Code. On the same machine, you can refer to the following code (without having to create a table first, the program will create the table by itself, and use the first row of data in Excel as the table field name ):

Dim Conn
Set conn = Createobject ("ADODB. Connection ")
Conn. Open ("driver = {SQL Server}; server = localhost; uid = sa; Pwd = sa; database = hwtemp ;")
SQL = "select * into newtable from OpenDataSource ('Microsoft. jet. oledb.4.0 ', 'Data source = "C: \ book1.xls" "; User Properties = Excel 5.0 ')... [sheet1 $]"

Conn.exe cute (SQL)

Conn. Close
Set conn = nothing

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.