偶然看到UDL,決定看一下其用法。
UDL:通用數據鏈接。此文件中提供 OleDbConnection 的連接信息。也就是說UDL只能在OleDbConnection中使用。
微軟不建議使用UDL
因為UDL 文件未加密,會以明文形式公開連接字符串信息。因為 UDL 文件對您的應用程序來說是一個基於文件的外部資源,所以無法使用 .NET Framework 保護該文件。
用法:
在桌面上建一個名為conn的txt文本文件,然后將后綴名改為udl 。雙擊它,打開相應連接界面,根據界面提示配置連接信息。這里選擇的是Oralce。
配置成功后UDL內容為:
[
oledb
]
; Everything after this line is an OLE DB initstring
Provider =OraOLEDB.Oracle. 1;Password =gsc;Persist Security Info =True; User ID =gsc;Data Source =NDEV
; Everything after this line is an OLE DB initstring
Provider =OraOLEDB.Oracle. 1;Password =gsc;Persist Security Info =True; User ID =gsc;Data Source =NDEV
代碼中使用此UDL時,ConnectionString 需要為“File Name =”+UDL地址。
private
void GetData()
{
using (OleDbConnection oleconn = new OleDbConnection())
{
oleconn.ConnectionString = @"File Name =" + Server.MapPath("conn.udl");
OleDbCommand olecommand = new OleDbCommand();
olecommand.Connection = oleconn;
olecommand.CommandText = " select * from CUSTOMIZATION ";
oleconn.Open();
OleDbDataAdapter oleadapter = new OleDbDataAdapter();
oleadapter.SelectCommand = olecommand;
DataSet ds = new DataSet();
oleadapter.Fill(ds);
}
}
{
using (OleDbConnection oleconn = new OleDbConnection())
{
oleconn.ConnectionString = @"File Name =" + Server.MapPath("conn.udl");
OleDbCommand olecommand = new OleDbCommand();
olecommand.Connection = oleconn;
olecommand.CommandText = " select * from CUSTOMIZATION ";
oleconn.Open();
OleDbDataAdapter oleadapter = new OleDbDataAdapter();
oleadapter.SelectCommand = olecommand;
DataSet ds = new DataSet();
oleadapter.Fill(ds);
}
}