2015-11-09 93 views
1

我想從Excel導入數據到Access,像這樣,當我的Access數據庫沒有密碼時它工作,但當它有密碼時不工作。 我從互聯網上覆制這段代碼。將Excel數據導入到Access數據庫

我必須在哪裏放置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 
+0

塔卡麻辣sugestia 「C:\ FWS \ Database1.accdb,False,False,「MS Access; PWD = password」) – Poof

回答

0

從快速搜索:

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;" 

或者

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

注意:這些未經測試。

1

更改cmd.commandText是這樣的:

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

請用你的數據庫密碼,像我code.my密碼爲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()