数据库连接字在Web.config里的用法
在asp.net中的WEB程序的设置中我们必须用到Web.config来存储数据库连接字.事实上这是个很好的做法,因为可以省去我们很多的麻烦还可以帮助我们避免不必要的错位,是的很多情况下我就是这样做.它通过XML来记录这些信息.具体的是在<appSettings>....</appSettings>这个标记中来记录的.这里请看一个ORACLE的例子如下:
<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>
而在你的应用程序中你只要这样写,就可以了,如下:
string conn = ConfigurationSettings.AppSettings["ORACLEConnectionString"];
OleDbConnection myConnection = new OleDbConnection(conn);
<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>
而在你的应用程序中你只要这样写,就可以了,如下:
string conn = ConfigurationSettings.AppSettings["ORACLEConnectionString"];
OleDbConnection myConnection = new OleDbConnection(conn);