人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

July 25, 2006

DataTable在.net2.0中的增强

Filed under: SQL&DB Accessing

–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);

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/07/25/p131/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham