ch10_06.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 53 行

CS
53
字号
using System; 
using System.Data; 
using System.Data.ADO; 

public class CH10_6
{ 
   public static void Main () 
   { 
       // Define the connection string
       string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=CH10.MDB"; 
       
       // Define our selection criteria
       string strRec = "SELECT Contact.Name, Contact.State, TaxRate.[Tax Rate] ";
       strRec += "FROM Contact INNER JOIN TaxRate ON Contact.State = TaxRate.State ";
       strRec += " WHERE (((Contact.State)=\"WI\"))";
  
       // Create the connection object, using our connection string
       ADOConnection myConn = new ADOConnection(strConn); 

       // Create the command object from our selection criteria string       
       ADOCommand myCmd = new ADOCommand( strRec, myConn ); 

       // Try to execute the command       
       ADODataReader outData = null; 
       try 
       { 
            // This will do the connection to the database
            myConn.Open(); 
	    
            // This will run our query and put the result in outData
            myCmd.Execute( out outData ); 
	

           // See if we got something back
           if ( outData != null ) 
           { 
                 while (outData.Read() ) 
                 { 
                     Console.WriteLine( "State: {0}", outData["State"]); 
                     Console.WriteLine( "Rate: {0}", outData["Tax Rate"]); 
                     Console.WriteLine( "Name: {0}", outData["Name"]); 
                 } 
           } 
    
       } 
       catch ( Exception e )
       {
          Console.WriteLine("Exception {0}", e );
       }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?