跟上时代吧。。。Ado.net2.0新特性:
In ADO.NET 2.0, you can load DataReader directly into DataSet or DataTable. Similarly you can get DataReader back from DataSet or DataTable. DataTable is now having most of the methods of DataSet. For example, WriteXML or ReadXML methods are now available in DataTable also. A new method "Load" is available in DataSet and DataTable, using which you can load DataReader into DataSet/DataTable. In other way, DataSet and DataTable is having method named "getDataReader" which will return DataReader back from DataTable/DataSet. Even you can transfer between DataTable and DataView. Check out the following example:
Dim dr As SqlDataReader
Dim conn As New SqlConnection(Conn_str)
conn.Open()
Dim sqlc As New SqlCommand("Select * from Orders", conn)
dr = sqlc.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable("Orders")
dt.Load(dr)
直接定义SqlDataAdapter用fill方法填充DataSet就可以。
SqlConnection con = new SqlConnection("连接字符串");
SqlDataAdapter sda = new SqlDataAdapter("SQL查询语句",con);
DataSet ds = new DataSet();
sda.Fill(ds,"表名");
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader 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();
}