Database Code
  Home arrow Database Code arrow Page 4 - A Closer Look at ADO.NET: The Connection O...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE CODE

A Closer Look at ADO.NET: The Connection Object
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 26
    2005-07-26

    Table of Contents:
  • A Closer Look at ADO.NET: The Connection Object
  • The Example
  • The ConnectionString Property Setting
  • Common Errors While Connecting
  • Using VS.NET to Create a SqlConnection Object

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Closer Look at ADO.NET: The Connection Object - Common Errors While Connecting


    (Page 4 of 5 )

    Although the code to establish a connection to the database is simple, many problems can happen. For example, the server doesn't exist or the database doesn't exist, which cause us problems when running the code. For this reason we will use the try/catch/finally statement to catch the exception and print the exception message to the console window. Here is what the code looks like:

    using System;
    // reference to the namespace that contains most
    // of the classes that form the ADO.NET Architecture
    using System.Data;
    // referene to the namespace of the SQL Server .NET Data provider
    using System.Data.SqlClient;

    namespace AdoApp
    {
      class Class1
      {
        static void Main(string[] args)
        {
          SqlConnection SqlConn1 = new SqlConnection();
          SqlConn1.ConnectionString = "Server=(local);Database=pubs;User ID=sa;Password=;";
          try
          {
            // We use the Open() method to establish the connection
            SqlConn1.Open();
            SqlCommand SqlComm1 = new SqlCommand();
            SqlComm1.Connection = SqlConn1;
            SqlComm1.CommandText = "Select * from titles";

            SqlDataReader SqlReader = SqlComm1.ExecuteReader(); 
            while(SqlReader.Read())
            {
              Console.Write(SqlReader["title_id"]);
              Console.Write("--");
              Console.Write(SqlReader["title"]);
              Console.WriteLine();
            } 

          }
          catch(Exception ex)
          {
            Console.WriteLine(ex.Message); 
          }
          finally
          { 
            // here we call the Close() method to close the Connection
            SqlConn1.Close();
            Console.ReadLine();
          } 
        }
      }
    }

    Now stop the SQL Server, then run the code, and you will get the next exception message printed to the console window after 15 seconds. 

    In the Connection String, modify the database name from pubs to pub and run the application again.

    As you can see, the try/catch/finally statement is good to use with the connection object. Note that we close the connection object in the finally block.

    More Database Code Articles
    More By Michael Youssef


     

    DATABASE CODE ARTICLES

    - Deployment of the MobiLink Synchronization M...
    - MobiLink Synchronization Wizard in SQL Anywh...
    - Finding Matching Records in Data Access Pages
    - Using the AccessDataSource Control in VS 2005
    - A Closer Look at ADO.NET: The Command Object
    - A Closer Look at ADO.NET: The Connection Obj...
    - Using ADO to Communicate with the Database, ...
    - Code Snippets: Counting Records
    - Constraints In Microsoft SQL Server 2000
    - Multilingual entries into a DB and to be dis...
    - Getting A List of Tables From SQL Server
    - SQL Server Database Creator - .NET Version
    - ADO Recordset Paging
    - Two combos, one textbox example
    - Discussion & Listserv Module by Mike Eck...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT