Hi,

People can you please help me on how to start the connections using SQL Server and C#.NET.
I have the following tools in my PC but I don't know how to start,from the very beginning.
Can you please send me a link on how to start.

Badly needed.
Your help will be very much appreciated.

Thanks in advance

Hi, try this code.

Using a SqlConnection in C#

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// 
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

        SqlDataReader rdr = null;
        //use try catch so it can handle the exception and your application won't crash.
        try
        {
            //Open the connection
            conn.Open();

            //Pass the connection to a command object
            SqlCommand cmd = new SqlCommand("select * from Orders", conn);

            //
            //Use the connection
            // get query results
            rdr = cmd.ExecuteReader();

            // print the OrderID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        catch(Exception ex)
         {
               Console.Write(ex.Message);
        }
        //put the close method in finally, we did this because whatever error that would occur,
        //we are pretty much sure that all the objects will be close.
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            //Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}

Hope this starting guide would help you.

This article has been dead for over six months. Start a new discussion instead.