.net
  當前位置:首頁 > 技術支持 > .net
數據庫連接字在Web.config裏的(de)用(yòng)法
發布時(shí)間:2006-05-15 浏覽:3143次
  

在asp.net中的(de)WEB程序的(de)設置中我們必須用(yòng)到Web.config來(lái)存儲數據庫連接字.事實上這(zhè)是個(gè)

很好的(de)做(zuò)法,因爲可(kě)以省去我們很多(duō)的(de)麻煩還(hái)可(kě)以幫助我們避免不必要的(de)錯位,是的(de)很多(duō)情況下(xià)

我就是這(zhè)樣做(zuò).它通(tōng)過xml來(lái)記錄這(zhè)些信息.具體的(de)是在<appsettings>....</appsettings>這(zhè)個(gè)

标記中來(lái)記錄的(de).這(zhè)裏請看一個(gè)oracle的(de)例子如下(xià):

<appsettings>
<add key="ORACLEConnectionString" value="Provider=OraOLEDB.Oracle.1;
Persist Security Info=False;Password=blah;User ID=greg;Data Source=sph;" />
<add key="SQLConnectionString" value="data source=SQL1;initial catalog=ID_V;
integrated security=SSPI;persist security info=False;workstation id=TH03D374;
packet size=4096"/>
<appSettings>

而在你的(de)應用(yòng)程序中你隻要這(zhè)樣寫,就可(kě)以了(le),如下(xià):

string conn = ConfigurationSettings.AppSettings["ORACLEConnectionString"];
OleDbConnection myConnection = new OleDbConnection(conn);

很輕松是嗎?不必每次都輸入同樣的(de)連接字,也(yě)不要記住那些討(tǎo)厭的(de)信息了(le),隻需要起一個(gè)好記的(de)

名字就可(kě)以.

好了(le)下(xià)面我在來(lái)給出其他(tā)的(de)一些連接字

mysql的(de)連接字:
ConnectionString = "Data Source=localhost;" +
"Database=mySQLDatabase;" +
"User ID=myUsername;" +
"Password=myPassword;" +
"Command Logging=false";
OLE DB的(de):
IBM AS/400 OLE DB 的(de)

" VB.NET
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=IBMDA400.DataSource.1;" & _
"Data source=myAS400DbName;" & _
"User Id=myUsername;" & _
"Password=myPassword"
oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

jet OLE DB 的(de)

" VB.NET
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\myPath\myJet.mdb;" & _
"User ID=Admin;" & _
"Password="
oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

oracle OLE DB 的(de)

" VB.NET
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=OraOLEDB.Oracle;" & _
"Data Source=MyOracleDB;" & _
"User ID=myUsername;" & _
"Password=myPassword"
oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

sql Server OLE DB 的(de)

" VB.NET
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=sqloledb;" & _
"Data Source=myServerName;" & _
"Initial Catalog=myDatabaseName;" & _
"User Id=myUsername;" & _
"Password=myPassword"
oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

sybase ASE OLE DB 的(de)

" VB.NET
Dim oOleDbConnection As OleDb.OleDbConnection
Dim sConnString As String = _
"Provider=Sybase ASE OLE DB Provider;" & _
"Data Source=MyDataSourceName;" & _
"Server Name=MyServerName;" & _
"Database=MyDatabaseName;" & _
"User ID=myUsername;" & _
"Password=myPassword"
oOleDbConnection = New OleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()

sql Server在System.Data.SqlClient

sqlconnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString = "Data Source=(local);" +
"Initial Catalog=mySQLServerDBName;" +
"Integrated Security=SSPI";
oSQLConn.Open();
 

 
 
    
 
 
版權所有 © 2005-2024 安徽問法信息技術有限公司  
地址:安徽省合肥市蜀山區(qū)望江西路69号西湖國際廣場(chǎng) 電話(huà):0551-64935878 郵箱:admin@ajsoft.cn