保存数据库连接登录名和密码

时间:2015-05-06 18:14:33

标签: sql-server database excel

我有一个Excel文件连接到SQL数据库,每次打开文件时都会问我登录ID和密码,有什么方法可以存储登录信息。

由于

1 个答案:

答案 0 :(得分:0)

如果您在工作簿中使用OLEDB连接,则可以在宏中存储用户名和密码:

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=SQLOLEDB.1;Persist Security Info=True;User ID='Id Here'; Password=''Password Here; Data Source= 'DB Name here' ;Use Procedure for Prepare=1" _
     , _
    ";Auto Translate=True;Packet Size=4096;Workstation ID="your workstationid";Use Encryption for Data=False;Tag with column collation when p" _
    , "ossible=False;Initial Catalog=your catalog"), Destination:=Range("where you want data")). _
    QueryTable
    .CommandType = xlCmdSql
    .CommandText = 'your string or query'
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=True

End With