DataTable在.net2.0中的增强
Post by "aloneplayer", 2006-10-12, 14:46-----------------------------------------------------
--dataTable可以独立于dataset而存在,
--dataTable可从datareader中读取数据:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Create a Command
using (SqlCommand command = new SqlCommand(Sql, connection))
{
// Call ExecuteReader to return a DataReader
using (SqlDataReader reader = command.ExecuteReader())
{
// Create a DataTable
DataTable table = new DataTable();
// Fill DataTable
table.Load(reader, LoadOption.OverwriteChanges);
// Display data in GridView
dataGridView1.DataSource = table;
}
}
}
--也可用Adapter填充
SqlDataAdapter adapter = new SqlDataAdapter(Sql, connection);
DataTable table = new DataTable("Employees");
adapter.Fill(table);
--用DataTableReaderCreate(it is disconnected)从datatable中读取数据:
DataTableReader dtReader = table.CreateDataReader();
while (dtReader.Read())
{
str = dtReader.GetValue(0).ToString();
}
如果
DataTableReader dtReader = dataset.CreateDataReader();
dtRader会读取DataSet中所有DataTable的数据.
--可以merge多个data table
dtTable1.Merge(dtTable2);
--Serialize的支持,从而可以在web method中传递DataTable
DataTable table = new DataTable();
table.Load(reader, LoadOption.OverwriteChanges);
// 设置序列化的格式,default为SerializationFormat.Xml
table.RemotingFormat = SerializationFormat.Binary;
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream("Data.txt", FileMode.OpenOrCreate);
bf.Serialize(fs, table);
页:
[1]