如何在ASP.NET中使用多个SqlDataReader
我在使用另一个如何在ASP.NET中使用多个SqlDataReader,asp.net,Asp.net,我在使用另一个SqlDataReaderconnection2的内部使用SqlDataReaderconnection1。因此,连接打开问题发生连接1打开。如何解决?请提供解决方案 首先创建不同的连接,datareader。。。反对 如果未读取数据,请关闭连接datareader 示例代码 string connectionString1 = "Data Source=localhost;Integrated security=SSPI;Initial Catalog=AdventureWork
SqlDataReader
connection2的内部使用SqlDataReader
connection1。因此,连接打开问题发生连接1打开。如何解决?请提供解决方案
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();
请分享你的代码。此外,connection1和connection2都指向同一个数据库?如果它是
SqlDataReader
,那么给它命名为connection1
是非常糟糕的,并且违反了“最小意外原则”:如果我看到一个名为connection1
的变量,我会马上假设这是一个SqlConnection
,但肯定不是SqlDataReader
。。。。。。。。你应该给那些reader1
和reader2
之类的名字!!