<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:
第一种方法设置:
<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"]);
<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!