asp.net - 如何在 ASP.NET 中使用多个 SqlDataReader
问题描述
我SqlDataReader
在里面使用connection1,我使用另一个SqlDataReader
connection2。所以连接打开问题发生连接1打开。如何解决?请提供解决方案
解决方案
- 首先创建不同的连接,datareader...对象
- 如果未读取数据,则关闭连接数据读取器
示例代码
string connectionString1 = "Data Source=localhost;Integrated security=SSPI;Initial Catalog=AdventureWorks;";
string connectionString2 = "Data Source=localhost;Integrated security=SSPI;Initial Catalog=AdventureWorks;";
//Create the command
string sqlSelect1 = "SELECT TOP 5 CustomerID, AccountNumber FROM Sales.Customer";
string sqlSelect2 = "SELECT TOP 5 CustomerID, AccountNumber FROM Sales.Customer";
//open the connection
SqlConnection objConn1 = new SqlConnection(connectionString1);
SqlConnection objConn2 = new SqlConnection(connectionString2);
// Create the command and open the connection
SqlCommand objcommand1 = new SqlCommand(sqlSelect1, objConn1);
SqlCommand objcommand2 = new SqlCommand(sqlSelect2, objConn2);
objConn1.Open();
objConn2.Open();
// Create the DataReader to retrieve data
SqlDataReader DR1 = objcommand1.ExecuteReader())
SqlDataReader DR2 = objcommand2.ExecuteReader())
while (DR1.Read())
{
if(DR2.Read())
{
//YOUR CODE
}
else
{
DR2.Close();
}
}
DR1.Close();
DR2.Close();
objConn1.Close();
objConn2.Close();
推荐阅读
- android - 番石榴依赖错误
- python - 过滤多级分组结果
- angular - HttpTestingController expectOne 匹配 URL 错误
- python - WTForms/flaskforms 无法通过提交按钮正确检索数据
- ios - 在 UICollectionView 中向 AVPlayerViewController 添加图像
- r - 从拟合的 lm 或 glm [R] 获取每个因子水平(以及交互作用)中的数据数量
- dynamics-crm - 当 MS 动态返回 HTTP/1.1 500 内部服务器错误时如何检索 http 响应正文?
- bazel - Bazel 的 cc_library 包含路径如何扩展?
- sql-server - Django中的每个用户数据库身份验证
- javascript - 文本区域线检测