1、ADO.NET结构:

    两个核心组件:DataSet、.NET数据提供程序(Connection、Command、DataReader和DataAdapter四个对象)。

    DataSet是独立于任何数据源的数据访问,因此可用于多种不同的数据源,用于XML数据或独立于管理应用程序本地的数据;DataSet包含一个或多个DataTable对象的集合;DataSet和XML之间是相互兼容的。


2、.NET数据提供程序:

    2.1、Connection:提供与数据源的连接

    2.2、Command:使用户能够访问用于返回数据、修改数据、运行存储过程以及发送或检索参数信息的数据库命令。

    2.3、DataReader:从数据源中提供高性能的数据流。

    2.4、DataAdapter:提供连接DataSet对象和数据源的桥梁。并且DataAdapter使用Command对象在数据源中执行SQL命令,        以便将数据加载到DataSet中,并使对DataSet中数据的更改与数据源保持一致。

    

    2.5、程序集:using System.Data


3、使用ADO连接到数据源

    3.1、连接SQL Server 7.0及以上版本,用SQL Server .NET提供的SqlConnection对象;若要连接OLE DB数据源或SQL Server 7.0一下版本,建议用OLE DB.NET数据提供的OleDbConnection对象。

    例:

/*使用SQL Server .NET SQL 连接 Server数据库*/

//集成安全连接

SqlConnection nwindConn = new SqlConnection("Data Source = localhost; Integrated Security = SSPI;"

+ "Initial Catalog = northwind");


nwindConn.Open();


//信任机制连接

SqlConnection myConn = new SqlConnection();

myConn.ConnectionString = "user id = xx; password = xx; initial catalog = northwind;

data source = local; Connect Timeout = 30";


myConn.Open();


/*使用OLE DB .NET SQL 连接 Server数据库*/

//与SQL Server .NET的区别:

    //Provider是必须的关键字

     //不支持URL、RemoteProvider和Remote Server关键字

    

OleDbConnection nwindConn = new OleDbConnection();

nwindConn.ConnectionString = "Provider = SQLOLEDB; Data Source = localhost;"

+ "Integrated Security = SSPI; Initial Catalog = northwind";


nwindConn.Open();


4、Command命令

4.1、代码
例:(SqlClient)
    SqlConnection nwindConn = new SqlConnection("……");
    SqlCommand catCMD = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn);
    
     (OleDb)
     OleDbConnection nwindConn = new OleDbConnection("……");
     OleDbCommand catCMD = new OleDbCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

4.2、属性
    当以数据流的形式返回结果时,使用ExecuteReader可返回DataReader对象;使用ExecuteScalar可返回单个值;
    使用ExecuteNonQuery可执行不返回行的命令。

4.3、将存储过程用于Command对象
    使用ADO.NET Command对象的Parameters集合可以显式地定义存储过程参数并访问输出参数和返回值。
     将Command对象的CommandType设置为StoredProdedure,就可以使用Parameters集合来定义参数。
     示例代码:
     //SalesByCategories是存储过程名

         (SqlClient)
    SqlConnection nwindConn = new SqlConnection("……");
    SqlCommand salesCMD = new SqlCommand("SalesByCategories", nwindConn);
     salesCMD.CommandType = CommandType.StoredProcedure;
     //构造Parameter对象
     SqlParameter myParm = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15);
     myParm.Value = "Beverages";
     nwindConn.Open();
    
     //使用DataReader对象将结果打印到控制台
     SqlDataReader myReader = salesCMD.ExecuteReader();
     Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));
     while (myReader.Read())
     {
          Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
     }
    
     myReader.Close();
     nwindConn.Close();
    
    (OleDb)
    OleDbConnection nwindConn = new OleDbConnection("……");
    OleDbCommand salesCMD = new OleDbCommand("SalesByCategories", nwindConn);
     salesCMD.CommandType = CommandType.StoredProcedure;
     //构造Parameter对象
     OleDbParameter myParm = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15);
     myParm.Value = "Beverages";
     nwindConn.Open();
    
     //使用DataReader对象将结果打印到控制台
     OleDbDataReader myReader = salesCMD.ExecuteReader();
     Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));
     while (myReader.Read())
     {
          Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
     }
    
     myReader.Close();
     nwindConn.Close();

    


5、使用DataReader检索数据


    5.1、DataReader对象使用方法如下:


          * 使用 Command 对象的 ExecuteReader 方法可以从数据源中检索行,并返回一个DataReader对象。


          * 使用 DataReader 对象的 Read 方法可以从查询结果中获取行。


          * 通过向 DataReader 传递列的名称或序号引用,可以访问返回行的每一行。


          * 为了实现最佳性能,DataReader 提供了一系列方法,如 GetDateTime、GetDouble、GetGuid、GetInt32 等,


            使用上述方法能够访问本机数据类型的列值。如果在基础数据类型未知时使用类型化访问器方法,


            将减少在检索列值时所需的类型转换量。


          * 每次使用完 DataReader 对象后,都应该调用 Close 方法。


          ---如果 Command 对象包含输出参数或返回值,那么在 DataReader 关闭之前,将无法访问这些输出参数或返回值。


             当 DataReader 打开后,该 DataReader 将以独占方式使用 Connection 对象。在初始 DataReader 关闭之前,将无法对 Connection 执行任何命令,包括创建另一个 DataReader。


    


     5.2、输出单个结果集


         SqlCommand myCommand = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn);


          SqlDataReader myReader = myCommand.ExecuteReader();


          while (myReader.Read())


          {


               Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));


          }


          myReader.Close();


         


     5.3、使用 DataReader 实例的 NextResult 方法来按顺序循环访问 Command 对象检索的多个结果集。


         //使用两条SQL查询语句构造返回多行结果集的 Command对象


         SqlCommand myCommand = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;"


          + "SELECT EmployeeID, LastName FROM Employees", nwindConn);


          nwindConn.Open();


          SqlDataReader myReader = myCommand.ExecuteReader();


          do



          {


               Console.WriteLine("\t{0}\t{1}", myReader.GetName(0), myReader.GetName(1));


               while (myReader.Read())


               {


                    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));


               }


          } while (myReader.NextResult());


          myReader.Close();    


        nwindConn.Close();    


    


     5.4、从 DataReader 中获取架构信息


         开发人员可以使用 DataReader 实例的 GetSchemaTable 方法检索有关当前结果集的架构信息。GetSchemaTable将       返回一个填充了行和列的 DataTable 对象,这些行和列包含当前结果集的架构信息。对于结果集的每一列,D啊他Table都将包含一行。架构表行的每一列都映射到在结果集中返回的列的属性,其中 ColumnName 是属性的名称,而列的值为属性的值。


         


          5.4.1、代码:


              DataTable schemaTable = myReader.GetSchemaTable();


               foreach (DataRow myRow in schemaTable.Rows)


               {


                    foreach (DataColumn myCol in schemaTable.Columns)


                    {


                         Console.WriteLine(myCol.ColumnName + "=" + myRow[myCol]);


                    }


                    Console.WriteLine();


               }