Wednesday 11 April 2012

Data Reader In ADO.NET


using System;
using System.Data.SqlClient;

class Program
{
static void Main()
{
SqlConnection con=new SqlConnection();
con.ConnectionString="server=.;database=student;integrated security=true;";
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="SELECT * FROM student";
SqlDataReader reader;
con.Open();
reader=cmd.ExecuteReader();

Console.WriteLine("Sno\tSname\t\tCourse\tDuration");
Console.WriteLine("-------------------------------------------------------------------");
while(reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",reader["Sno"],reader["Sname"],reader["Cname"],reader["duration"]);
Console.WriteLine();
}
reader.Close();
con.Close();
}
}

DataBase Connectivity in c#


using System;
using System.Data.SqlClient;

class Program
{
static void Main()
{
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
int n;
string command;

con.ConnectionString="server=.;database=Student;integrated security=true;";
cmd.Connection=con;

// command="CREATE TABLE student(Sno INT PRIMARY KEY IDENTITY(1,1),Sname VARCHAR(30),Cname VARCHAR(30),Duration INT)";
// cmd.CommandText=command;

// con.Open();
// Console.WriteLine("Connected to Sql Server");
// cmd.ExecuteReader();
// Console.WriteLine("Table Created Successfully");
// con.Close();

command="INSERT INTO student(Sname,Cname,Duration) VALUES ('SMRITI DABGAR','.NET',90)";
cmd.CommandText=command;

con.Open();
n=cmd.ExecuteNonQuery();
Console.WriteLine("{0} Records Inserted Successfully",n);
con.Close();

command="DELETE FROM student WHERE sno=1";
cmd.CommandText=command;

con.Open();
n=cmd.ExecuteNonQuery();
Console.WriteLine("{0} Records Deleted Successfully",n);
con.Close();

command="UPDATE student SET sname='ANKUR BHATNAGAR' WHERE sno=2";
cmd.CommandText=command;

con.Open();
n=cmd.ExecuteNonQuery();
Console.WriteLine("{0} Records Updated Successfully",n);
con.Close();

command="SELECT count(*) FROM student WHERE Cname='.NET'";
cmd.CommandText=command;

con.Open();
object ob=cmd.ExecuteScalar();
Console.WriteLine("Total No Of Records in .NET is: {0}",(int)ob);
con.Close();
}
}

StringBuilder in C#


//String classs and StringBuilder Class Comparision
using System;
using System.Text;
class Program
{
static void Main()
{
StringBuilder sb=new StringBuilder();

Console.WriteLine("\n......................String Builder Example......................\n");

sb.Append("This is a example of string");

Console.WriteLine("\nActual String is: '{0}'",sb.ToString());

sb.Replace("string","StringBuilder");
Console.WriteLine("\nAfter Replace String is: '{0}'",sb.ToString());

sb.Insert(0,"Extra ");
Console.WriteLine("\nAfter Insert String is: '{0}'",sb.ToString());

sb.Remove(0,5);
Console.WriteLine("\nAfter Remove String is: '{0}'",sb.ToString());

Console.WriteLine("\nStart Time: {0}",DateTime.Now.ToString("T"));
for(int i=0;i<100000;i++)
sb.Append(i);
Console.WriteLine("\nEnd Time: {0}",DateTime.Now.ToString("T"));

Console.WriteLine("\n......................String Example......................\n");
string s1="";

Console.WriteLine("\nStart Time: {0}",DateTime.Now.ToString("T"));
for(int i=0;i<100000;i++)
s1=s1+i;
Console.WriteLine("\nEnd Time: {0}",DateTime.Now.ToString("T"));
}
}

String Funtions in C#


// Program to demonstrate sting funtions
//Instance level funtions: ToUpper(), ToLower(), Trim(), Split(), IndexOf(), LastIndexOf(), Substing(), Contains
//Static Funtions: Concat()
using System;
class Program
{
static void Main()
{
string s1,s2;
s1="Hello";
s2="World";

s1=string.Concat(s1,s2);
Console.WriteLine("Cancat Result: {0}",s1);

Console.WriteLine("Upper Result: {0}",s1.ToUpper());
Console.WriteLine("Lower Result: {0}",s1.ToLower());

bool b=s1.Contains("World");
Console.WriteLine("Contains Result: {0}",b);

int n=s1.IndexOf("llo");
Console.WriteLine("IndexOF Result: {0}",n);

n=s1.LastIndexOf('l');
Console.WriteLine("LastIndexOf Result: {0}",n);

Console.WriteLine("Substring Result: {0}",s1.Substring(3));
Console.WriteLine("substring Result: {0}",s1.Substring(3,5));

s2="                    Ankur Bhatnagar                    ";
Console.WriteLine("Before Trim Result: {0}",s2.Length);
s2=s2.Trim();
Console.WriteLine("After Trim Result: {0}",s2.Length);

s2="ASP.NET,C#.NET,VB.NET";
string[] str=s2.Split(',');
Console.WriteLine("Split Result:");
foreach(string item in str)
{
Console.WriteLine("{0}",item);
}
}
}