简体   繁体   中英

Import Excel data to Access database

I want to import data from Excel to Access like this, it's working when my Access database has no password, but not working when it has a password. I copy this code from internet.

Where do I have to put the password of Access?

Dim Access As String = "C:\FWS\Database1.accdb"
Dim Excel As String = "C:\New folder\2h.xlsx"
' Dim connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Excel & ";Extended Properties=Excel 8.0;"

Dim connect As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excel + ";Extended Properties=""Excel 12.0 Xml;HRD=NO"""


    Using conn As New OleDbConnection(connect)
        Using cmd As New OleDbCommand()
            cmd.Connection = conn
            cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & "].[Password=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using

Change the cmd.commandText to be like this:

cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";
    PWD=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"

From a quick search:

https://www.connectionstrings.com/access/

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;"

http://www.sqlstrings.com/ms-access-connection-strings.htm

"Driver= {MicrosoftAccessDriver(*.mdb)}; DBQ=C:\App1\Your_Database_Name.mdb;Uid=Your_Username; Pwd=Your_Password;"

Or

"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\Your_Database_Name.mdb; User Id=admin; Password="

NOTE: These are untested.

please use your db password like my code.my password is 123.

    Using cmd As New OleDbCommand()
    cmd.Connection = conn
    cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";PWD=**123**].[Sheet1] SELECT * FROM [Sheet1$]"
    If conn.State = ConnectionState.Open Then
       conn.Close()
    End If
    conn.Open()
    cmd.ExecuteNonQuery()
    MessageBox.Show("succussfully updated")
    Form4.Show()
    Me.Hide()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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