"Ado.net 2.0 technical insider
Sqlconnection
A class specifically designed to communicate with a Microsoft SQL Server database. It represents the connection to the database and serves as the starting point for creating queries and transactions.
Example:
StringStrconn;
Strconn= @"Data Source =. \ sqlexpress;" + "Initial catalog = northwind; intergrated SECURITY = true;"
Sqlconnection CN= NewSqlconnection (strconn );
Open the sqlconnection object:
CN. open ();
Close the sqlconnection object:
CN. Close ();
Sqlcommand class
In the ADO. Net object model, the sqlcommand class is used to execute queries.
The sqlcommand object allows different types of queries in the execution queue. Some sqlcommand object classes use result sets to obtain data, while others modify the content or structure of the data storage area.
Create a sqlcommand object
You can create a sqlcommand object in three ways.
Example:
String Strconn, strsql;
Strconn = @" Data Source =. \ sqlexpress; " + " Initial catalog = northwind; trusted_connection = yes; " ;
Strsql = " Select customerid, companyName from MERs " ;
Sqlconnection CN = New Sqlconnection (strconn );
CN. open ();
Sqlcommand cmd;
// Use a non-parameter Constructor
CMD = New Sqlcommand ();
Cmd. Connection = CN;
Cmd. commandtext = Strsql;
// Using parameterized Constructors
CMD = New Sqlcommand (strsql, CN );
// Use the createcommand method of the connection object
CMD = CN. createcommand ();
Cmd. commandtext = Strsql;
"