ADO.NET 2.0 操作实例

基于基类的提供者模型
      提供者程序
     1.得到machine.config文件中所有注册的DbProvider 
      this.dataGridView1.DataSource = DbProviderFactories.GetFactoryClasses();
     2.指定一个连接信息,知道某种数据库程序,来得到连接字符串

 DbConnection conn  =   null ;
               string  strConnectionString  =  ADODOTNET2.Properties.Settings.Default.NorthwindConnectionString;
            DbProviderFactory f 
=  DbProviderFactories.GetFactory( " System.Data.SqlClient " );
            conn 
=  f.CreateConnection();
            conn.ConnectionString 
=  strConnectionString;
               this .label1.Text  =  conn.ConnectionString.ToString();
     3.枚举所有网内的所有的Data Sources
 Cursor.Current  =  Cursors.WaitCursor;
            
try
            
{
                
// see this method in the example above
                DbProviderFactory f = DbProviderFactories.GetFactory("System.Data.SqlClient");
                
// if our factory supports creating a DbConnection, return it.
                if (f.CanCreateDataSourceEnumerator)
                
{
                    DbDataSourceEnumerator enu 
= f.CreateDataSourceEnumerator();
                    DataTable t 
= enu.GetDataSources();
                    
// uses chooses a Data Row r
                    
//DataRow r = t.Rows[0];
                    
//string dataSource = (string)r["ServerName"];
                    
//if (r[InstanceName] != null)
                    
//    dataSource += ("\\" + r["InstanceName"]);
                    //// this method is defined below
                    //RewriteConnectionStringAndUpdateConfigFile(f, dataSource);

                    
this.dataGridView1.DataSource = t;
                }

                
else
                    Console.WriteLine(
"Source must be changed manually");
            }

            
finally
            
{
                Cursor.Current 
= Cursors.Default;
            }

你可能感兴趣的:(.net)