首页 > 编程 > .NET > 正文

ADO.NET 2.0 DataSet和DataTable 新功能新特性

2024-07-10 13:11:51
字体:
来源:转载
供稿:网友

ado.net 2.0 dataset和datatable 新功能新特性

1.新的索引引擎更快的执行效率
   下面这段代码在2003中需要157秒,在2005中只要11秒就可以完成:


dataset ds = new dataset();

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

cursor.current = cursors.waitcursor;

datetime datbegin = datetime.now;

random rand = new random();
int i, intvalue;
datarow dr;

for (i = 1; i <= 500000; i++)
{
    try
    {
        intvalue = rand.next();

        dr = ds.tables[0].newrow();

        dr["id"] = intvalue;
        dr["value"] = intvalue;

        ds.tables[0].rows.add(dr);
    }
    catch { }
}

cursor.current = cursors.default;

messagebox.show("elapsed time: " + (datetime.now - datbegin).seconds.tostring());
messagebox.show("count = " + ds.tables[0].rows.count.tostring());

2.dataset可以序列化为二进制文件

string connstr = "server=(local);database=northwind;integrated security=true;async=true";

dataset ds = new dataset();
sqldataadapter dadpt = new sqldataadapter("select * from [order details]", connstr);
dadpt.fill(ds);

binaryformatter bf = new binaryformatter();
filestream fs = new filestream(@"c:/xml1.txt",filemode.openorcreate);

ds.remotingformat = serializationformat.binary;

bf.serialize(fs,ds);    

3.更独立的datatable

datatable write xml  string connstr = "server=(local);database=northwind;integrated security=true;async=true";
sqldataadapter dadpt = new sqldataadapter("select * from [order details]", connstr);
datatable dt = new datatable("customer");
dadpt.fill(dt);

dt.writexml(@"c:/datatable.xml",true);
dt.writexmlschema(@"c:/datatableschema.xml");   datatable read xml
streamreader sr = new streamreader(@"c:/datatableschema.xml");

datatable dt = new datatable();
dt.readxmlschema(sr);

dt.readxml(new streamreader(@"c:/datatable.xml"));

this.datagridview1.datasource = dt;    datatable merge
string connstr = "server=(local);database=northwind;integrated security=true;async=true";
sqldataadapter dadpt = new sqldataadapter("select * from customers", connstr);
datatable dt = new datatable("customer");
dadpt.fill(dt);

sqldataadapter dadpt1 = new sqldataadapter("select * from customers", connstr);
datatable dt1 = new datatable("customer1");
dadpt1.fill(dt1);

dt.merge(dt1);

this.datagridview1.datasource = dt;  datatable load datareader
string connstr = "server=(local);database=northwind;integrated security=true;async=true";
sqlconnection conn = new sqlconnection(connstr);
conn.open();
sqlcommand cmd = new sqlcommand("select * from [order details]", conn);
sqldatareader dr = cmd.executereader();

datatable dt = new datatable("customer");
dt.load(dr);

this.datagridview1.datasource = dt;

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表