在asp.net中的WEB程序的设置中我们必须用到Web.config来存储数据库连接字.事实上这是个
很好的做法,因为可以省去我们很多的麻烦还可以帮助我们避免不必要的错位,是的很多情况下
我就是这样做.它通过XML来记录这些信息.具体的是在<appSettings>....</appSettings>这个
标记中来记录的.这里请看一个ORACLE的例子如下:
<appSettings>
<addkey="ORACLEConnectionString"value="Provider=OraOLEDB.Oracle.1;
PersistSecurityInfo=False;Password=blah;UserID=greg;DataSource=sph;"/>
<addkey="SQLConnectionString"value="datasource=SQL1;initialcatalog=ID_V;
integratedsecurity=SSPI;persistsecurityinfo=False;workstationid=TH03D374;
packetsize=4096"/>
<appSettings>
而在你的应用程序中你只要这样写,就可以了,如下:
stringconn=ConfigurationSettings.AppSettings["ORACLEConnectionString"];
OleDbConnectionmyConnection=newOleDbConnection(conn);
很轻松是吗?不必每次都输入同样的连接字,也不要记住那些讨厌的信息了,只需要起一个好记的
名字就可以.
好了下面我在来给出其他的一些连接字
MYSQL的连接字:
ConnectionString="DataSource=localhost;"+
"Database=mySQLDatabase;"+
"UserID=myUsername;"+
"Password=myPassword;"+
"CommandLogging=false";
OLEDB的:
IBMAS/400OLEDB的
'VB.NET
DimoOleDbConnectionAsOleDb.OleDbConnection
DimsConnStringAsString=_
"Provider=IBMDA400.DataSource.1;"&_
"Datasource=myAS400DbName;"&_
"UserId=myUsername;"&_
"Password=myPassword"
oOleDbConnection=NewOleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()
JETOLEDB的
'VB.NET
DimoOleDbConnectionAsOleDb.OleDbConnection
DimsConnStringAsString=_
"Provider=Microsoft.Jet.OLEDB.4.0;"&_
"DataSource=C:/myPath/myJet.mdb;"&_
"UserID=Admin;"&_
"Password="
oOleDbConnection=NewOleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()
OracleOLEDB的
'VB.NET
DimoOleDbConnectionAsOleDb.OleDbConnection
DimsConnStringAsString=_
"Provider=OraOLEDB.Oracle;"&_
"DataSource=MyOracleDB;"&_
"UserID=myUsername;"&_
"Password=myPassword"
oOleDbConnection=NewOleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()
SQLServerOLEDB的
'VB.NET
DimoOleDbConnectionAsOleDb.OleDbConnection
DimsConnStringAsString=_
"Provider=sqloledb;"&_
"DataSource=myServerName;"&_
"InitialCatalog=myDatabaseName;"&_
"UserId=myUsername;"&_
"Password=myPassword"
oOleDbConnection=NewOleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()
SybaseASEOLEDB的
'VB.NET
DimoOleDbConnectionAsOleDb.OleDbConnection
DimsConnStringAsString=_
"Provider=SybaseASEOLEDBProvider;"&_
"DataSource=MyDataSourceName;"&_
"ServerName=MyServerName;"&_
"Database=MyDatabaseName;"&_
"UserID=myUsername;"&_
"Password=myPassword"
oOleDbConnection=NewOleDb.OleDbConnection(sConnString)
oOleDbConnection.Open()
SQLServer在System.Data.SqlClient
SqlConnectionoSQLConn=newSqlConnection();
oSQLConn.ConnectionString="DataSource=(local);"+
"InitialCatalog=mySQLServerDBName;"+
"IntegratedSecurity=SSPI";
oSQLConn.Open();