ADO.NET 2.0 中的新增 DataSet 功能--MSDN(downmoon翻译成Csharp)
原文地址:http://www.microsoft.com/china/MSDN/library/data/dataAccess/adonetdatasetenhance.mspx?mfr=true
本文主要帮助读者了解有关 .NET Framework DataSet 类以及与它密切相关的类中的新增 ADO.NET 2.0 功能的知识。这些更改包括对 DataSet、DataTable 和 DataView 类的功能和性能增强。
因为原文代码用的是vb.net,阅读起来不便,邀月翻译成Csharp2005,方便大家阅读。呵呵!
private
void
button1_Click(
object
sender,EventArgse)
...
{
//下列代码在vs2005下执行40秒!
//在vs2003下执行30分钟!!增加44倍!!
DataSetds = new DataSet();
System.DateTimetime1 = new System.DateTime();
System.TimeSpants = new TimeSpan();
System.Timers.Timertm = new System.Timers.Timer();
tm.Enabled = true ;
tm.Interval = 1000 ;
int i;
int SPsecond = 0 ;
DataRowdr;
ds.Tables.Add( " BigTable " );
ds.Tables[ 0 ].Columns.Add( " ID " ,Type.GetType( " System.Int32 " ));
ds.Tables[ 0 ].Columns[ " ID " ].Unique = true ;
ds.Tables[ 0 ].Columns.Add( " Value " ,Type.GetType( " System.Int32 " ));
WaitLabel.Visible = true ;
this .Cursor = Cursors.WaitCursor;
this .Refresh();
time1 = DateTime.Now;
Randomrand = new Random();
// Randomri=newRandom(unchecked((int)DateTime.Now.Ticks));
int value;
for ( int k = 1 ;k <= 1000000 ;k ++ )
... {
try 
... {
value = rand.Next();
dr = ds.Tables[ 0 ].NewRow();
dr[ " ID " ] = value;
dr[ " Value " ] = value;
ds.Tables[ 0 ].Rows.Add(dr);
}
catch (Exceptionex)
... {
}
}
WaitLabel.Visible = false ;
this .Cursor = this .DefaultCursor;
ts = DateTime.Now - time1;
SPsecond = ts.Seconds;
MessageBox.Show( " ElapsedTime: " + SPsecond.ToString() + " Seconds " );
MessageBox.Show( " count= " + ds.Tables[ 0 ].Rows.Count);
}
private
void
button2_Click(
object
sender,EventArgse)
...
{
XMLFormat();
}
private
void
XMLFormat()
...
{
DataSetds = new DataSet();
System.Data.SqlClient.SqlDataAdapterda = new System.Data.SqlClient.SqlDataAdapter( " select*from[orderdetails] " ,GetConnectionString());
da.Fill(ds);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatterbf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
string FileName = " c:/ " + DateTime.Now.ToString( " yyyyMMddhhmmsslll " ) + " xml20.txt " ;
System.IO.FileStreamfs = new System.IO.FileStream(FileName,System.IO.FileMode.CreateNew);
bf.Serialize(fs,ds);
MessageBox.Show( " 生成成功 " + FileName + " " + " 文件大小(b): " + fs.Length.ToString());
}
private
void
BinaryFormat()
...
{
DataSetds = new DataSet();
System.Data.SqlClient.SqlDataAdapterda = new System.Data.SqlClient.SqlDataAdapter( " select*from[orderdetails] " ,GetConnectionString());
da.Fill(ds);
//
下句使生成数据减少80%左右
ds.RemotingFormat = SerializationFormat.Binary;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatterbf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
string FileName = " c:/ " + DateTime.Now.ToString( " yyyyMMddhhmmsslll " ) + " Binary20.txt " ;
System.IO.FileStreamfs = new System.IO.FileStream(FileName,System.IO.FileMode.CreateNew);
bf.Serialize(fs,ds);
MessageBox.Show( " 生成成功 " + FileName + " " + " 文件大小(b): " + fs.Length.ToString());
}
private
string
GetConnectionString()
...
{
// returnMySettings.Value.NorthwindConnection;
return " server=10.0.0.121;database=northwind;uid=sa;pwd=sa " ;
}
private
void
button3_Click(
object
sender,EventArgse)
...
{
BinaryFormat();
}
private
void
button4_Click(
object
sender,EventArgse)
...
{
getGridViewData();
}
private
void
getGridViewData()
...
{
try 
... {
SqlConnectionconnection = new SqlConnection(GetConnectionString());
SqlCommandcommand = new SqlCommand( " SELECT*fromcustomers " ,connection);
connection.Open();
System.Data.SqlClient.SqlDataReaderdr = command.ExecuteReader();
// FilltablewithdatafromDataReader
System.Data.DataTabledt = new DataTable();
dt.Load(dr,LoadOption.OverwriteChanges);
// Displaythedata
dataGridView1.DataSource = dt;
} 
catch (SqlExceptionex)
... {
MessageBox.Show(ex.Message);
}
}
邀月总结:2.0对1.1的改进,不仅在DataSet,同样在GridView对于DataGrid性能的改进。应尽量摒弃Datagrid!这也是微软在2.0下默认不加载DataGrid的原因。
助人等于自助! 3w@live.cn
ADO.NET 2.0性能
本文介绍ADO.NET 2.0中DataSet类及其相关类的功能改进,包括性能提升及新特性。通过示例展示了如何使用C#进行数据填充、序列化及GridView优化。
54

被折叠的 条评论
为什么被折叠?



