Eariler tonight I started playing with some more ADO.NET 2.0 code and just started mixing it up. It appears the ASP.NET 2.0 team has done such a good job with ASP.NET 2.0 that you don't really need to write any code for it anymore, so I can spend my time elsewhere. There are two new ADO.NET 2.0 Features that are pretty cool - although I probably won't be using the first much:
Generic DB Providers:
| private DbConnection cn; private void button1_Click(object sender, EventArgs e) { DbProviderFactory myFactory = DbProviderFactories.GetFactory("System.Data.SqlClient"); DbDataSourceEnumerator eNum = myFactory.CreateDataSourceEnumerator(); DataTable providers = eNum.GetDataSources(); dgv.DataSource = providers; //At this point, it shows Bill_2k3 - the only instance of SQL Server //Running internally on my network using (cn = myFactory.CreateConnection()) { cn.ConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxxxxx"; try { cn.Open(); DbCommand cmd = myFactory.CreateCommand(); cmd.Connection = cn; cmd.CommandText = "SELECT * FROM Customers"; DbDataReader dr = cmd.ExecuteReader(); } //Notice we're catching a Generic DBException object catch (DbException ex) { Debug.Assert(false, ex.ToString()); } finally { cn.Close(); } } } |
| private DbConnection cn; private void button1_Click(object sender, EventArgs e) { DbProviderFactory myFactory = DbProviderFactories.GetFactory("System.Data.SqlClient"); DbDataSourceEnumerator eNum = myFactory.CreateDataSourceEnumerator(); DataTable providers = eNum.GetDataSources(); dgv.DataSource = providers; //At this point, it shows Bill_2k3 - the only instance of SQL Server //Running internally on my network using (cn = myFactory.CreateConnection()) { DbConnectionStringBuilder csb = new DbConnectionStringBuilder(; cn.ConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind"; try { cn.Open(); DbCommand cmd = myFactory.CreateCommand(); cmd.Connection = cn; cmd.CommandText = "SELECT * FROM Customers"; DbDataReader dr = cmd.ExecuteReader(); //cn.Close(); DataTable dt = new DataTable("BillsTest"); Debug.WriteLine(dt.Rows.Count.ToString());// Returns 0 proving it's empty dt.Load(dr); Debug.WriteLine(dt.Rows.Count.ToString());// Returns 91 //That's right, we just filled a DataTable without a DataAdapter and without iterating through //something to fill it. DbDataReader drTwo = dt.GetDataReader(); Debug.Assert(drTwo.HasRows); while (drTwo.Read()) { lb.Items.Add((string)drTwo.GetString(0)); Debug.WriteLine(lb.Items.Count.ToString());// Returns 91 } Debug.WriteLine(dt.Rows.Count.ToString());// Returns 91 } //Notice we're catching a Generic DBException object catch (DbException ex) { Debug.Assert(false, ex.ToString()); } cn.Close(); } } |