these are the basic Steps
1.Connect to database(IDbConnection) SQlConnection
2.Execute SQL command object(IDbCommand) SqlCommand
3.Read and compute (IDataReader) SqlDataReder
4.Release resources
usingSystem.Data;....
1.) Declare connection;
try
{
// optional 1.) Create connection to data source;
2.) Execute SQL commands;
3.) Compute result;
4.) Release resources;
} catch( Exception ) {Handle exception or dispatch it; } finally { try { // optional4.) Close connection; } catch(Exception) { Handle exception; }} IDataReader ExecuteReader(...)//Property CommandTextonly contains SQL SELECT statements Int ExecuteNonQuery() //Property CommandTextcontains INSERT, UPDATE, DELETE, ... cmd.CommandText= "UPDATEEmplsSETCity = ’Seattle’ WHERElD=8"; intaffectedRows= cmd.ExecuteNonQuery();
IDbCommandcmd= newSqlCommand("SELECTcount(*) FROM Empls",new SqlConnection(connStr));
cmd.Connection.Open();
intcount = (int) cmd.ExecuteScalar();
cmd.Connection.Close(); |