ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

Command Constructors
Previous Home Next
  • New():- Creates a new, default instance of the Data Command.

  • New(Command): Creates anew Data Command with the Command Text set to the string specified in command.

  • New(Command, Connection): Creates a new Data Command with the Command Text set to the string specified in Command and the Connection property set to the SqlConnection specified inConnection.

  • New(Command, Connection, Transaction): Creates anew Data Command with the CommandText set to the string specified in Command the Connection property set to the Connection specified in Connection,and the Transactionproperty set to the transaction specified in Transaction..

 

Example of SqlCommand Object

usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
classSqlCommandexample
{
SqlConnectionconn;
publicSqlCommandDemo()
{
//Instantiatetheconnection
conn=newSqlConnection(
"DataSource=computer;InitialCatalog=
Emp;IntegratedSecurity=SSPI");
}
//callmethodsthatdemoSqlCommandcapabilities
staticvoidMain()
{
SqlCommandexamplescd=newSqlCommandexampleo();
Console.WriteLine();
Console.WriteLine("CategoriesBeforeInsert");
Console.WriteLine("------------------------");
//useExecuteReadermethod
scd.ReadData();
//useExecuteNonQuerymethodforInsert
scd.Insertdata();
Console.WriteLine();
Console.WriteLine("CategoriesAfterInsert");
Console.WriteLine("------------------------------");
scd.ReadData();
//useExecuteNonQuerymethodforUpdate
scd.UpdateData();
Console.WriteLine();
Console.WriteLine("CategoriesAfterUpdate");
Console.WriteLine("------------------------------");
scd.ReadData();
//useExecuteNonQuerymethodforDelete
scd.DeleteData();
Console.WriteLine();
Console.WriteLine("CategoriesAfterDelete");
Console.WriteLine("------------------------------");
scd.ReadData();
//useExecuteScalarmethod
intnumberOfRecords=scd.GetNumberOfRecords();
Console.WriteLine();
Console.WriteLine("NumberofRecords:{0}",numberOfRecords);
}
///<summary>
///useExecuteReadermethod
///</summary>
publicvoidReadData()
{
SqlDataReaderrdr=null;
try
{
//Opentheconnection
conn.Open();
//1.Instantiateanewcommandwithaqueryandconnection
SqlCommandcmd=newSqlCommand("selectEmpNamefromEmp",conn);
//2.CallExecutereadertogetqueryresults
rdr=cmd.ExecuteReader();
//printtheCategoryNameofeachrecord
while(rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
//closethereader
if(rdr!=null)
{
rdr.Close();
}
//Closetheconnection
if(conn!=null)
{
conn.Close();
}
}
}
///<summary>
///useExecuteNonQuerymethodforInsert
///</summary>
publicvoidInsertdata()
{
try
{
//Opentheconnection
conn.Open();
//preparecommandstring
stringinsertString=@"
insertintoEmp
(EmpName,post)
values('Raj','A\c')";
//1.Instantiateanewcommandwithaqueryandconnection
SqlCommandcmd=newSqlCommand(insertString,conn);
//2.CallExecuteNonQuerytosendcommand
cmd.ExecuteNonQuery();
}
finally
{
//Closetheconnection
if(conn!=null)
{
conn.Close();
}
}
}
///<summary>
///useExecuteNonQuerymethodforUpdate
///</summary>
publicvoidUpdateData()
{
try
{
//Opentheconnection
conn.Open();
//preparecommandstring
stringupdateString=@"
updateEmp
setEmpName='Anil'
whereEmpName='Raj'";
//1.Instantiateanewcommandwithcommandtextonly
SqlCommandcmd=newSqlCommand(updateString);
//2.SettheConnectionproperty
cmd.Connection=conn;
//3.CallExecuteNonQuerytosendcommand
cmd.ExecuteNonQuery();
}
finally
{
//Closetheconnection
if(conn!=null)
{
conn.Close();
}
}
}
///<summary>
///useExecuteNonQuerymethodforDelete
///</summary>
publicvoidDeleteData()
{
try
{
//Opentheconnection
conn.Open();
//preparecommandstring
stringdeleteString=@"
deletefromEmp
whereEmpName='Ashish'";
//1.Instantiateanewcommand
SqlCommandcmd=newSqlCommand();
//2.SettheCommandTextproperty
cmd.CommandText=deleteString;
//3.SettheConnectionproperty
cmd.Connection=conn;
//4.CallExecuteNonQuerytosendcommand
cmd.ExecuteNonQuery();
}
finally
{
//Closetheconnection
if(conn!=null)
{
conn.Close();
}
}
}
///<summary>
///useExecuteScalarmethod
///</summary>
///<returns>numberofrecords</returns>
publicintGetNumberOfRecords()
{
intcount=-1;
try
{
//Opentheconnection
conn.Open();
//1.Instantiateanewcommand
SqlCommandcmd=newSqlCommand("selectcount(*)fromEmp",conn);
//2.CallExecuteScalartosendcommand
count=(int)cmd.ExecuteScalar();
}
finally
{
//Closetheconnection
if(conn!=null)
{
conn.Close();
}
}
returncount;
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: