ASP.net通过web.config设置数据库连接
基本例子:
<configuration>
<!-- application specific settings -->
<appSettings>
<add key=ConnectionString value=server=localhost;uid=sa;pwd=;database=store />
</appSettings>
<configuration>
public SqlDataReader GetReviews(int productID) {
// 创建Connection和Command对象实例
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings[ConnectionString]);
SqlCommand myCommand = new SqlCommand(ReviewsList, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
// 参数
SqlParameter parameterProductID = new SqlParameter(@ProductID, SqlDbType.Int, 4);
parameterProductID.Value = productID;
myCommand.Parameters.Add(parameterProductID);
// 执行
myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// 返回结果
return result;
数据库连接;return true>
本文来源【学网】网站链接是http://www.xue5.com
oledb:
第一种方法设置:
<connectionStrings><add name = "mdbconn" [String, Required, Collection Key] connectionString = "" [String, Required] providerName = "System.Data.SqlClient" [String] />
</connectionStrings>
读取:
string strConnection = ConfigurationManager.ConnectionStrings["mdbconn"].ToString();
第二种方法设置:
<appSettings>
<add key="strcon" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/Backup/我的文档/Visual Studio 2005/WebSites/4ec/App_Data/company.mdb"/>
</appSettings>
<connectionStrings/>
读取:
OleDbConnection con = new OleDbConnection(ConfigurationSettings.AppSettings["strcon"]); sqlserver:
<connectionStrings>
<add name="SQLCONNECTIONSTRING01" connectionString="data Source=SIMON;database=mysimon;user id=sa;pwd=11111111" providerName="System.Data.SqlClient"></add>
<add name="SQLCONNECTIONSTRING02" connectionString="data source=SIMON; Initial Catalog=new01; Integrated Security=True" providerName="System.Data.SqlClient"></add>
</connectionStrings>
方法一SQLCONNECTIONSTRING01是通过用户“sa”来登陆SQL server数据库,访问表格mysimon
方法二SQLCONNECTIONSTRING02是通过windows验证方式登陆SQL server,不需要用户和密码,访问表格new01,应该只能是访问本机安装的SQL server!
- 点赞
- 收藏
- 分享
- 文章举报
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET通过配置Web.config文件提高手动绑定数据库效率的一个简单方法,不用每次手动连接数据库地址,或者数据库地址变更时只改一处即可。
- asp.net 下web.config中配置数据库连接
- ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池
- ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池
- asp.net数据库连接web.config配置
- ASP.NET web.config中,数据库连接字符串的加密与解密
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET 中Web.config 配置连接数据库
- asp.net数据库连接web.config配置
- Asp.net中Web.config连接字符串及配置数据库sqlserver 浮兮vip解析
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET WEB.CONFIG 配置数据库连接
- ASP.NET实验三:读取web.config连接数据库
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- ASP.NET中使用web.config配置数据库连接
- asp.net 通过web.config 文件设置网站的mime类型
- ASP.NET中使用web.config配置数据库连接