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} |