数据库连接字在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);其他的配置操作类似
在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);其他的配置操作类似
它把自己的配置放在一个独立的XmlNode下,并在‘<configSections>’中“注册”。如:
<configuration>
<configSections>
<section name="ApplicationConfiguration" type="Duwamish7.SystemFramework.ApplicationConfiguration, Duwamish7.SystemFramework" />
</configSections>
<ApplicationConfiguration>
<!-- Settings specific to the Duwamish application -->
<add key="Duwamish.DataAccess.ConnectionString" value="server=(local);User ID=Duwamish7_login;Password=password;database=Duwamish7;Connection Reset=FALSE" />
<add key="Duwamish.Web.EnablePageCache" value="True" />
<add key="Duwamish.Web.PageCacheExpiresInSeconds" value="3600" />
<add key="Duwamish.Web.EnableSsl" value="False" />
</ApplicationConfiguration>
</configuration>它有一个专门的配置管理类(ApplicationConfiguration : IConfigurationSectionHandler)来读取这些信息。最后,在global.asax 中写如下代码:
void Application_OnStart()
{
ApplicationConfiguration.OnApplicationStart(Context.Server.MapPath( Context.Request.ApplicationPath ));
// 它还调用别的配置文件。你也可以参照着做。
string configPath = Path.Combine(Context.Server.MapPath( Context.Request.ApplicationPath ),"remotingclient.cfg");
if(File.Exists(configPath))
RemotingConfiguration.Configure(configPath);
}我觉得这种方式很不错,推荐给你。
看看System.Xml空间
<appSettings>
<add key="DSN" value="data source=你的机器名;uid=sa;pwd=sa;database=数据库名" />
</appSettings>