myReader.Close(); myConnection.Close();看msdn:备注 如果想通过使用 OleDbDataReader 来将关联的 OleDbConnection 用于任何其他目的,则必须显式调用 Close 方法。示例 [Visual Basic, C#] 以下示例创建一个 OleDbConnection、一个 OleDbCommand 和一个 OleDbDataReader。该示例读取全部数据,并将其写到控制台。最后,该示例先关闭 OleDbDataReader,再关闭 OleDbConnection。[Visual Basic] Public Sub ReadMyData(myConnString As String) Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders" Dim myConnection As New OleDbConnection(myConnString) Dim myCommand As New OleDbCommand(mySelectQuery, myConnection) myConnection.Open() Dim myReader As OleDbDataReader myReader = myCommand.ExecuteReader() ' Always call Read before accessing data. While myReader.Read() Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _ + myReader.GetString(1)) End While ' always call Close when done reading. myReader.Close() ' Close the connection when done with it. myConnection.Close() End Sub[C#] public void ReadMyData(string myConnString) { string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders"; OleDbConnection myConnection = new OleDbConnection(myConnString); OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection); myConnection.Open(); OleDbDataReader myReader; myReader = myCommand.ExecuteReader(); // Always call Read before accessing data. while (myReader.Read()) { Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1)); } // always call Close when done reading. myReader.Close(); // Close the connection when done with it. myConnection.Close(); }
myConnection.Close();看msdn:备注
如果想通过使用 OleDbDataReader 来将关联的 OleDbConnection 用于任何其他目的,则必须显式调用 Close 方法。示例
[Visual Basic, C#] 以下示例创建一个 OleDbConnection、一个 OleDbCommand 和一个 OleDbDataReader。该示例读取全部数据,并将其写到控制台。最后,该示例先关闭 OleDbDataReader,再关闭 OleDbConnection。[Visual Basic]
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
Dim myConnection As New OleDbConnection(myConnString)
Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As OleDbDataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
+ myReader.GetString(1))
End While
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub[C#]
public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
OleDbConnection myConnection = new OleDbConnection(myConnString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
myConnection.Open();
OleDbDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
DataReader.Close()的同时你的 connection 也会close