• 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;
                }
  • 相关阅读:
    gson和fastjson
    Hive和HBase的区别
    mac 比较两个文件
    mysql 查找在另一张表不存在的数据
    mysql 根据一张表更新另一张表
    shell调试
    目标
    百度在线面试总结
    20170109| javascript记录
    php-fpm问题
  • 原文地址:https://www.cnblogs.com/ghd258/p/264791.html
Copyright © 2020-2023  润新知