ASP.NET实验二:读取web.config连接数据库
2013-04-02 14:44
134 查看
目的:1、熟悉手动修改web.config信息,
2、熟悉通过asp.net进行读取web.config信息;
3、熟悉数据库连接流程。
要求:1、在web.config中新建AppSetting节点, database = School, dbuser = sa, dbpassword = longlt ;
2、在asp.net中创建连接,读取数据
web.config关键代码:
index.aspx.cs关键代码:
连接主代码:
知识点:web.config中的常用节点小结:
ASP.NET中常用3个配置节点:<customErrors>、<connectionStrings>、<appSettings>的用法
1、<customErrors>:
<customErrors>属于<system.web>里的节,它允许你在发生各种HTTP错误时配置应用程序的行为。
如:
<customErrors mode="RemoteOnly" defaultRedirect="CommonErrorPage.aspx">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
其中mode可以设置为三种模式:
1) On:自定义错误被启用,如果没有提供defaultRedirect属性,用户将看到一个一般的错误。
2) Off:自定义错误被禁用,用户将看到错误的详细信息。
3) RemoteOnly:本地的管理员能够看见真实的错误信息而不被重定向,但远程用户只能够看到被定向的信息提示页面。
在使用时需要注意两点:一是你在配置文件里定义的自定义错误设置只在ASP.NET处理请求时才有效;二是如果你的自定义页面发生错误,ASP.NET将不能够处理。它不会再次把用户转送同一页面,相反,它将会显示一个带有一般信息的普通客户端错误页面。
2、<connectionStrings>:
<connectionStrings>属于<configuration>里的节,它主要是为你的项目设置数据库连接字符串所用,可以在里面添加一个或者多个数据库连接字符串。
如:
<connectionStrings>
<add name="ConnectString" connectionString="server=.;database=Eipsoft.Test; uid=sa;pwd=mawei;"providerName="System.Data.SqlClient"/>
<add name="ConnectString1" connectionString="server=.;
database=Eipsoft.Test1;uid=sa;pwd=mawei;"providerName="System.Data.SqlClient"/>
</connectionStrings>
3、<appSettings>:最常用的一种
<appSettings>属于<configuration>里的节,它主要用于信息的自定义的设置。例如,可以在里面添加项目的版权信息、项目名称等。
<appSettings>
<!--系统用户配置信息-->
<add key="CustomerName" value="默认用户"/>
<add key="Title" value="系统名称"/>
</appSettings>
在ASP.NET中对web.config的操作:
(1)获取<appSettings>节点和<connectionStrings>的信息
//获取appSettings节点
WebConfigurationManager.AppSettings["CustomerName"];
//获取connectionStrings节点
WebConfigurationManager.ConnectionStrings["ConnectString"];
(2)在<appSettings>节点中添加新元素
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//在appSettings节点中添加元素
appSection.Settings.Add("newkey1", "newkey1's value");
appSection.Settings.Add("newkey2", "newkey2's value");
//保存
config.Save();
(3)修改和删除<appSettings>节点或属性
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//删除appSettings节点中的元素
appSection.Settings.Remove("newkey1");
//修改appSettings节点中的元素
appSection.Settings["newkey2"].Value = "修改newkey2的值";
config.Save();
注:知识点部分参考《ASP.NET4权威指南》
2、熟悉通过asp.net进行读取web.config信息;
3、熟悉数据库连接流程。
要求:1、在web.config中新建AppSetting节点, database = School, dbuser = sa, dbpassword = longlt ;
2、在asp.net中创建连接,读取数据
web.config关键代码:
<appSettings> <add key="dbname" value="School"/> <add key="dbuser" value="sa"/> <add key="dbpassword" value="longlt"/> </appSettings>
index.aspx.cs关键代码:
public String Get_AppSettings(string str)//读取web.config中AppSettings中的数据 { return (string)WebConfigurationManager.AppSettings[str]; } public string ConnectSqlServerStr() //返回建立连接字符串 { return "server=localhost;" + "uid=" + Get_AppSettings("dbuser") + ";pwd=" + Get_AppSettings("dbpassword") + ";database=" + Get_AppSettings("dbname"); }
连接主代码:
string strconn = ConnectSqlServerStr(); SqlConnection conn = new SqlConnection(strconn); string sql = "select * from students"; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); Response.Write("连接成功");
知识点:web.config中的常用节点小结:
ASP.NET中常用3个配置节点:<customErrors>、<connectionStrings>、<appSettings>的用法
1、<customErrors>:
<customErrors>属于<system.web>里的节,它允许你在发生各种HTTP错误时配置应用程序的行为。
如:
<customErrors mode="RemoteOnly" defaultRedirect="CommonErrorPage.aspx">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
其中mode可以设置为三种模式:
1) On:自定义错误被启用,如果没有提供defaultRedirect属性,用户将看到一个一般的错误。
2) Off:自定义错误被禁用,用户将看到错误的详细信息。
3) RemoteOnly:本地的管理员能够看见真实的错误信息而不被重定向,但远程用户只能够看到被定向的信息提示页面。
在使用时需要注意两点:一是你在配置文件里定义的自定义错误设置只在ASP.NET处理请求时才有效;二是如果你的自定义页面发生错误,ASP.NET将不能够处理。它不会再次把用户转送同一页面,相反,它将会显示一个带有一般信息的普通客户端错误页面。
2、<connectionStrings>:
<connectionStrings>属于<configuration>里的节,它主要是为你的项目设置数据库连接字符串所用,可以在里面添加一个或者多个数据库连接字符串。
如:
<connectionStrings>
<add name="ConnectString" connectionString="server=.;database=Eipsoft.Test; uid=sa;pwd=mawei;"providerName="System.Data.SqlClient"/>
<add name="ConnectString1" connectionString="server=.;
database=Eipsoft.Test1;uid=sa;pwd=mawei;"providerName="System.Data.SqlClient"/>
</connectionStrings>
3、<appSettings>:最常用的一种
<appSettings>属于<configuration>里的节,它主要用于信息的自定义的设置。例如,可以在里面添加项目的版权信息、项目名称等。
<appSettings>
<!--系统用户配置信息-->
<add key="CustomerName" value="默认用户"/>
<add key="Title" value="系统名称"/>
</appSettings>
在ASP.NET中对web.config的操作:
(1)获取<appSettings>节点和<connectionStrings>的信息
//获取appSettings节点
WebConfigurationManager.AppSettings["CustomerName"];
//获取connectionStrings节点
WebConfigurationManager.ConnectionStrings["ConnectString"];
(2)在<appSettings>节点中添加新元素
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//在appSettings节点中添加元素
appSection.Settings.Add("newkey1", "newkey1's value");
appSection.Settings.Add("newkey2", "newkey2's value");
//保存
config.Save();
(3)修改和删除<appSettings>节点或属性
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//删除appSettings节点中的元素
appSection.Settings.Remove("newkey1");
//修改appSettings节点中的元素
appSection.Settings["newkey2"].Value = "修改newkey2的值";
config.Save();
注:知识点部分参考《ASP.NET4权威指南》
相关文章推荐
- ASP.NET实验三:读取web.config连接数据库
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET中使用web.config配置数据库连接
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- asp.net加密web.config 加密数据库连接字符串
- ASP.NET中使用web.config配置数据库连接
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- asp.net数据库连接web.config配置
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- 【ASP.NET】将数据库连接字符串写在Web.config
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- ASP.NET连接数据库字符串,web config用
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- asp.net数据库连接web.config配置