Wrox Programmer Forums
|
BOOK: Professional C#, 2nd and 3rd Editions
This is the forum to discuss the Wrox book Professional C#, 3rd Edition by Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen; ISBN: 9780764557590
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C#, 2nd and 3rd Editions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2003, 03:33 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Where the sample code?

Where can I get/download the sample codes in the book 'Professional C#, 2nd Edition'?
 
Old June 27th, 2003, 04:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

http://www.wrox.com/dynamic/books/download.aspx
 
Old June 30th, 2003, 03:27 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.
 
Old August 20th, 2003, 07:51 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to comedyox
Default

On p522, a directory called "Chapter 9\01_ExecutingCommands" is cited but does not exist in the code download.

How do I get this code?

Ox.
 
Old August 20th, 2003, 11:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Dear friend: here u r;)
Code:
using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Data.OleDb;

/// <summary>
/// Corresponds to section titled 'Executing Commands' in Chapter 11
/// </summary>
public class ExecutingCommands
{
    /// <summary>
    /// SimpleDataAccess - show SQL & Stored Procs
    /// </summary>
    public static void Main ( )
    {
        // The following is the database connection string
                string source = Login.Connection ;

        // First section of code - using a SQL statement to select records
        ExecuteSql ( source ) ;

        // Second section - calling a stored procedure
        ExecuteStoredProc ( source ) ;

        // Third - batch statements
        ExecuteBatch ( source ) ;

        // Fourth - Return XML from SqlServer...
        ExecuteXml ( source ) ;

        // Fifth - full table
        ExecuteFullTable ( source ) ;

    }

    public static void ExecuteSql ( string source )
    {
        // And this is the SQL statement that will be issued
        string select = "SELECT ContactName,CompanyName FROM Customers";

        try
        {
            // Connect to the database...
            using ( SqlConnection conn=new SqlConnection(source) )
            {
                // Open the database connection
                conn.Open ( ) ;

                // Create the SQL command...
                SqlCommand        cmd = new SqlCommand ( select , conn ) ;

                // Construct the data reader
                using ( SqlDataReader    reader = cmd.ExecuteReader ( ) )
                {
                    // Output headings...
                    Console.WriteLine ( "*** SqlProvider ***" ) ;
                    Console.WriteLine ( "Output from direct SQL statement..." ) ;
                    Console.WriteLine ( ) ;
                    Console.WriteLine ( "CONTACT                        COMPANY" ) ;
                    Console.WriteLine ( "---------------------------------------------------------------------" ) ;

                    // And iterate through the data
                    while ( reader.Read ( ) )
                    {
                        Console.WriteLine ( "{0,-30} {1}" , reader[0] , reader[1] ) ;
                    }

                    reader.Close ( ) ;
                }

                conn.Close ( ) ;
            }
        }
        catch ( Exception e )
        {
            Console.WriteLine ( e.ToString( ) ) ;
        }
    }

    public static void ExecuteStoredProc ( string source )
    {
        // Connect to the database...
        using ( SqlConnection    conn = new SqlConnection(source) )
        {
            // Open the database connection
            conn.Open ( ) ;

            // Create the SQL command that links to a stored procedure
            SqlCommand    cmd = new SqlCommand ( "CustOrderHist" , conn ) ;

            // Set the type to stored procedure
            cmd.CommandType = CommandType.StoredProcedure ;

            // And add the parameter to the stored proc...
            cmd.Parameters.Add ( "@CustomerID" , "QUICK" ) ;

            // Construct the data reader
            using ( SqlDataReader    reader = cmd.ExecuteReader ( ) )
            {
                Console.WriteLine ( "" ) ;
                Console.WriteLine ( "*** SqlProvider ***" ) ;
                Console.WriteLine ( "Call NorthWind CustOrderHist stored proc for customer 'QUICK'..." ) ;
                Console.WriteLine ( ) ;
                Console.WriteLine ( "Product Name                       Quantity" ) ;
                Console.WriteLine ( "---------------------------------------------------------------------" ) ;

                // Iterate through the data
                while ( reader.Read ( ) )
                {
                    Console.WriteLine ( "{0,-34} {1}" , reader[0] , reader[1] ) ;
                }

                reader.Close ( ) ;

                Console.WriteLine ( ) ;
            }

            // Close the connection
            conn.Close ( ) ;
        }
    }

    protected static void ExecuteFullTable ( string source ) 
    {
        // Connect to the database...
        using ( OleDbConnection    conn = new OleDbConnection("Provider=SQLOLEDB;" + source) )
        {
            // Open the database connection
            conn.Open ( ) ;

            // Create the SQL command that links to a stored procedure
            OleDbCommand    cmd = new OleDbCommand ( "Categories" , conn ) ;

            // Set the type to TableDirect
            cmd.CommandType = CommandType.TableDirect;

            // Construct the data reader
            using ( OleDbDataReader    reader = cmd.ExecuteReader ( ) )
            {
                Console.WriteLine ( "" ) ;
                Console.WriteLine ( "*** OleDbProvider ***" ) ;
                Console.WriteLine ( "Listing all records in Categories table..." ) ;
                Console.WriteLine ( ) ;
                Console.WriteLine ( "ID  Name            Description" ) ;
                Console.WriteLine ( "---------------------------------------------------------------------" ) ;

                // Iterate through the data
                while ( reader.Read ( ) )
                {
                    Console.WriteLine ( "{0,-3} {1,-15} {2}" , reader[0] , reader[1], reader[2] ) ;
                }

                reader.Close ( ) ;

                Console.WriteLine ( ) ;
            }

            // Close the connection
            conn.Close ( ) ;
        }
    }

    protected static void ExecuteBatch ( string source )
    {
        string            select = "SELECT COUNT(*) FROM Customers;SELECT COUNT(*) FROM Products";

        // Connect to the database...
        using ( SqlConnection    conn = new SqlConnection(source) )
        {
            // Open the database connection
            conn.Open ( ) ;

            // Create the SQL command...
            SqlCommand        cmd = new SqlCommand ( select , conn ) ;

            // Construct the data reader
            using ( SqlDataReader    reader = cmd.ExecuteReader ( ) )
            {
                // Output headings...
                Console.WriteLine ( "*** SqlProvider ***" ) ;
                Console.WriteLine ( "Output from batched SQL statements" ) ;
                Console.WriteLine ( ) ;

                int        statement = 0 ;

                do
                {
                    statement++ ;

                while ( reader.Read ( ) )
                {
                    Console.WriteLine ( "Output from batch statement {0} is {1}" , statement , reader[0] ) ;
                }
                } while ( reader.NextResult ( ) ) ;

                reader.Close ( ) ;
            }

            conn.Close ( ) ;
        }
    }

    protected static void ExecuteXml ( string source )
    {
        string            select = "SELECT ContactName,CompanyName FROM Customers FOR XML AUTO";

        using ( SqlConnection    conn = new SqlConnection(source) )
        {
            // Open the database connection
            conn.Open ( ) ;

            // Create the SQL command...
            SqlCommand        cmd = new SqlCommand ( select , conn ) ;

            // Construct an Xml Reader
            XmlReader        xr = cmd.ExecuteXmlReader ( ) ;

            Console.WriteLine ( "" ) ;
            Console.WriteLine ( "*** SqlProvider ***" ) ;
            Console.WriteLine ( "Use ExecuteXmlReader with a FOR XML AUTO SQL clause" ) ;
            Console.WriteLine ( ) ;

            // Do something useful with the xml
            while ( xr.Read() )
            {
                Console.WriteLine ( xr.ReadOuterXml ( ) ) ;
            }

            // And close the connection
            conn.Close ( ) ;
        }
    }
}
Always:),
Hovik Melkomian.
 
Old August 21st, 2003, 02:58 AM
Registered User
 
Join Date: Jul 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to comedyox
Default

Thanks.

Although I have just found this source code buried in another sub-directory of the code download.

Editors please note - code download should be completely in sync with chapters of book and page citings should be accurate.

Ox





Similar Threads
Thread Thread Starter Forum Replies Last Post
want sample code mohiddin52 Access VBA 2 December 16th, 2006 04:54 AM
Where is the code of sample application? rogerthis BOOK: Professional Java Development with the Spring Framework 1 November 27th, 2006 01:26 AM
I want to download sample code! caolei Pro JSP 6 May 26th, 2006 10:36 AM
Need Sample code okshilato SQL Server 2005 0 March 28th, 2006 10:41 AM
Sample Code mirouvor Java Databases 1 January 2nd, 2006 02:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.