How to create a SQL Connection in C Sharp……….

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Database
{
class Program
{
static void Main(string[] args)
{
// String copy in notepad from property windows
string conString = @”Data Source=.\SQLEXPRESS;AttachDbFilename=””D:\visual studio2010\Database\Database\emp.mdf””;Integrated Security=True;User Instance=True” ;

SqlConnection con = new SqlConnection(conString);
con.Open();

string sql = “select Fname,Lname,Email,Mobile from employee where IsActive = 1”;

//Fire Querry
SqlCommand cmd = new SqlCommand(sql,con);

//Get Result of Querry
SqlDataReader rd = cmd.ExecuteReader();

//Iterate over all result
while(rd.Read())
{
string msg = string.Format(“Fname = {0} Lname = {1} Email = {2} \n Mobile = {3}”,rd[“Fname”],rd[“Lname”],rd[“Email”],rd[“Mobile”]);

Console.WriteLine(msg);
}

rd.Close();
con.Close();
Console.Read();

}
}
}

SHARE THE KNOWLEDGE


  

Advertisements

About Dinesh

I am engineering student........ I am selected in Microsoft Student Partner as MSP............

Posted on January 29, 2012, in C#, Knowledge. Bookmark the permalink. Leave a comment.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: