最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
在web.config中建立数据库连接
时间:2008-01-12 编辑:简简单单 来源:一聚教程网
在asp.net应用程序下找到web.config文件,在前面加入下面的代码:
......
在aspx文件里面建立连接:
public SqlDataReader GetReviews(int productID) {
// 创建Connection和Command对象实例
SqlConnection myConnection = new SqlConnection(System.Configuration.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;
}
......
在aspx文件里面建立连接:
public SqlDataReader GetReviews(int productID) {
// 创建Connection和Command对象实例
SqlConnection myConnection = new SqlConnection(System.Configuration.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;
}
相关文章
- java连接数据库(代码分享) 07-25
- 所有数据库连接方式的整理 05-20
- java怎么连接数据库 怎么解决的方法 02-17
- java怎么连接数据库?完整java开发中JDBC连接数据库代码和步骤 02-10
- NiceLabel怎样快速连接数据库打印条形码? 05-06
- java中jdbc连接 数据库代码 12-18