ADO.Net-Connecting sql ,executing quaries,displaying table using console Application

No.of Views1110
Bookmarked0 times
Downloads 
Votes0
By  poly666   On  16 Feb 2010 00:02:46
Tag : WPF , How to
ADO.Net-Connecting sql ,executing quaries,displaying table using console Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippets will give the to access sql server database with sqlconnection Object.



Code Sample

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

namespace ConsoleApplication53

{

class Program

{

static void Main(string[] args)

{

/*it will create a sqlconnection obj which is used for etablishing connection

with the database using ConnectionString property of class SqlClient*/

SqlConnection mycon = new SqlConnection();

/*Include 6 parameters

1)Server or Data Source means Sql server name

2)Data base or Initial Catalog means Database name created in Sql

3)User Id means the id used for accessing sql

4)Password

5)Integrated Security = if u set it to false then u have to give password and user name

it u set it to false then tere is no need for giving password and user id*/

mycon.ConnectionString = "Data Source=XPWINDOWS7;Database=Gniit;Integrated Security=True";

/*we r using open method of sqlconnection class*/

mycon.Open();

/*This is how we give commend that we want to be executed in Sql*/

SqlCommand cmd = new SqlCommand("Select * from Software",mycon);

/*This is a method of Sql data provider used to read data in Sql*/

SqlDataReader dr = cmd.ExecuteReader();

/*here we are displaying the values read by sqldatareader*/

while (dr.Read())

{

Console.Write(dr[0]);

Console.Write(" {0} ",dr[1]);

Console.Write(" {0} ",dr[2]);

Console.WriteLine();

}

Console.ReadLine();

/*Thanks for reading hope u have understood*/

/*\m/peace\m/*/

}

}

}

{/codecitation}

 
Sign Up to vote for this article
 
About Author
 
poly666
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-15 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top