.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

A simple c# program

Posted By:Gowthammanju       Posted Date: June 21, 2011    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

this code explains u about the building of basic program in c#.net with sql server

Here are the steps that you need to take in order to connect and performs some operation on a database:

1)Instantiate the SqlConnection.
2)Open the connection.
3)Pass the connection to other ADO.NET objects.
4)Perform database operations with the other ADO.NET objects.
5)Close the connection.

in header file use as :

using System;
using System.Data;
using System.Data.SqlClient;
in body use as:

class myfirstapplication
    static void Main()
        // 1. Instantiate the connection
        SqlConnection connection = new SqlConnection(
            "Data Source=(local);Initial Catalog=DevelopersIsland;Integrated Security=SSPI");

        SqlDataReader rdr = null;

            // 2. Open the connection

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

            // 4. Use the connection

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

            // print the User ID of each record
            while (rdr.Read())
            // close the reader
            if (rdr != null)

            // 5. Close the connection
            if (connection != null)
thats it


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend