Database Code
  Home arrow Database Code arrow Page 2 - 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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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 / 25
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    A Closer Look at ADO.NET: The Connection Object - The Example


    (Page 2 of 5 )

    Copy the following code and paste it into your VS.NET class file, then run the application.

    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=MichaelServer;Database=pubs;" +
    "Integrated Security=true";
          // 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();
          }

          // here we call the Close() method to close the Connection
          SqlConn1.Close();
          Console.ReadLine(); 
        }
      }
    }


    You will get the following results to the console when you run this code example. Note that you need to have SQL Server installed in order to run this example.

    In the above example, we have connected to the database pubs and issued a SQL Query (Select * from titles); we then printed out the results. For the purpose of our article I have instantiated two more objects in order to get the above results (to illustrate that we have established a connection to the database), but we will not discuss those objects in detail here. We will do that in another article. Let's take a look at the SqlConnection instantiation code:

    SqlConnection SqlConn1 = new SqlConnection();
    SqlConn1.ConnectionString = "Server=MichaelServer;Database=pubs;" +
    "Integrated Security=true";
    // We use the Open() method to establish the connection
    SqlConn1.Open();

    After we instantiated our SqlConn1 object, we set the ConnectionString property (which provides the information needed to connect to the database and begins manipulating its tables). Then we have opened the connection.

    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();
    }

    // here we call the Close() method to close the Connection
    SqlConn1.Close();

    The above code creates the object SqlComm1 of type SqlCommand. Actually, after we establish the connection we need to issue or execute a query or a stored procedure, so we can think of them as commands to send to the SQL Server to execute (again, we will discuss commands in another article). The next statement that follows sets the connection that the command uses.

    Of course we have set the SqlCommand.Connection property to our connection object SqlConn1. Next we need the command that we want to execute, which is a simple Select statement that selects all the rows of the table titles. Then we use an object of type SqlDataReader to read the records, and after that we close the connection. Now we need to discuss the ConnectionString property in detail.

    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 4 hosted by Hostway