New features of Ado.net 2.0 dataset and DataTable

Source: Internet
Author: User
Tags count datetime new features rand tostring
Ado

New features of Ado.net 2.0 dataset and DataTable

1. Faster execution efficiency of the new indexing engine
The following code takes 157 seconds in 2003 and can be completed in 2005 in 11 seconds:


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 can be serialized as a binary file

String connstr = "server= (local);d atabase=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. More independent DataTable

DataTable Write XML String connstr = "server= (local);d atabase=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 (The new StreamReader (@ "C:\dataTable.xml"));

This.dataGridView1.DataSource = DT; DataTable Merge
String connstr = "server= (local);d atabase=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);d atabase=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;



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.