.Net Basic

This blogs gives introduction to C#.NET programming for beginners. This blogs assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby!after Refer this blog

19 June 2008

Fill Into DataReader

Using Oledb
Step 1: Establishing Connection

OleDbConnection OCon=new OleDbConnection(StrCon);


//StrCon is a connection String

Step 2: Fill Into DataReader

Con.Open();

OleDbCommand OCmd=new OleDbCommand(StrQry,OCon);

OleDbDataReader Rdr=OCmd.ExecuteReader();


Using sqlclient
Step 1: Establishing Connection

SqlConnection OCon=new SqlConnection(StrCon);

//StrCon is a connection String

Step 2: Fill Into DataReader

Con.Open();

SqlCommand OCmd=new SqlCommand(StrQry,OCon);

SqlDataReader Rdr=OCmd.ExecuteReader();




Check the Table is Empty

Using Oledb
Step 1: Establishing Connection

OleDbConnection OCon=new OleDbConnection(StrCon);

//StrCon is a connection String

Step 2: Check Whether the Table is Empty

OCon.Open();

OleDbCommand OCmd = new OleDbCommand(StrQry, OCon); OleDbDataReader Rdr=OCmd.ExecuteReader();

if (Rdr.HasRows)

message(“table is not Empty”);

else

message(“table is Empty”);


Using Sqlclient


Step 1: Establishing Connection

SqlConnection OCon=new SqlConnection (StrCon);

//StrCon is a connection String

Step 2: Check Whether the Table is Empty

OCon.Open();

SqlCommand OCmd = new SqlCommand(StrQry, OCon);

SqlDataReader Rdr=OCmd.ExecuteReader();

if (Rdr.HasRows)

message(“table is not Empty”);

else

message(“table is Empty”);

Labels: ,

Execute Normal Query

Using Oledb
Execute Normal Query
Step 1
: Establishing Connection

OleDbConnection OCon=new OleDbConnection(StrCon);

//StrCon is a connection String

Step 2: Execute Normal Query

OCon.Open();

OleDbCommand OCmd = new OleDbCommand(strQry, OCon);

//StrQry is Query

OCmd.ExecuteNonQuery();

OCon.Close();


Using Sqlclient
Execute Normal Query
Step 1
: Establishing Connection

SqlConnection OCon=new SqlConnection(StrCon);

//StrCon is a connection String

Step 2: Execute Normal Query

OCon.Open();

SqlCommand OCmd = new SqlCommand(strQry, OCon);

//StrQry is Query

OCmd.ExecuteNonQuery();

OCon.Close();



Labels: , ,

Connect the Database

Using Oledb

Establishing Connection

OleDbConnection OCon=new OleDbConnection(StrCon);
//StrCon is Connection string
OCon.open();
//Some database transactions
OCon.Close();

Using SqlClient

Establishing Connection

SqlConnection OCon=new SqlConnection (StrCon);
//StrCon is Connection string
OCon.open();
//Some database transactions
OCon.Close();

Labels: , ,

Fill Into DataSet

Using Oledb

DataSet DtaSet=new DataSet();

OleDbDataAdapter OAdp = new OleDbDataAdapter(StrSQL, StrCon);

//StrCon is Connection string

//StrSQL is Sql Quories

OAdp.Fill(DtaSet);

Using SqlClient

DataSet DtaSet=new DataSet();

SqlDataAdapter OAdp = new SqlDataAdapter(StrSQL, StrCon);

//StrCon is Connection string

//StrSQL is Sql Quories

SqlAdp.Fill(DtaSet);






Fill into DataTable

Using Oledb

Fill DataTable

DataTable DtaTbl=new DataTable();

OleDbDataAdapter OAdp=new OleDbDataAdapter(StrSQL,StrCon);

OAdp.Fill(DtaTbl);



Using SqlClient

Fill DataTable

    DataTable DtaTbl=new DataTable();

SqlDataAdapter OAdp=new SqlDataAdapter(StrSQL,StrCon);

OAdp.Fill(DtaTbl);



Connection Strings for C#.net

Name Space For Oledb Connection

System.Data. SqlClient;

Connection strings

StrCon = “Data Source=myServerName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;”

Name Space For Oledb Connection

Using System.Data.OleDb;

For Excel

StrCon="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+AppDomain.CurrentDomain.BaseDirectory+ "DataBase Name; Password=;";

For MySql

StrCon="Provider=MySQLProv;Data Source=mydb;User Id=myUsername;Password=myPassword;"

For Oracle

StrCon="Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;"

For Oracle
StrCon="
Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;"

Labels: , ,