In many program writing, database programming is one of the most important programming survival skills. And all the language, the basic steps can be divided into 4, summed up to 4 words: "Even, life, hold, close."
1. "Lian"
That is, to "establish a connection", take C # as an example, with SqlConnection or OleDbConnection, you can set up the connection string to complete the connection step. Of course you can open the connection now, but it's better to put it in the back of the try...catch...finally.
2. "Life"
"Create a command" and establish a connection you need to write SQL statements using SqlCommand or OleDbCommand. Of course I place the DataAdapter in the command, similar to the SqlCommand.
3. "Practice"
Execute command, using the SqlDataReader or other execution object, to perform the command established in step 2nd.
4. "Off"
Close connection, you must close the connection in time to save resources after the execution completes.
code example:
Even
string strsql= "server=servername;database=dbname;integrated security=true";
SqlConnection sqlcon=new SqlConnection (strSQL);
Life
String strcmd= "select * from table";
SqlCommand sqlcmd=new SqlCommand (strcmd,sqlcon);
Holds
try
{sqlcon.open ();
SqlDataReader Sqldr=sqlcmd.executereader ();
while (Sqldr.read ())
{Response.wirte (sqldr[1].tostring () + "<br>");}
catch (Exception ee)
{Response.Write (ee.tostring ());}
Close
finally
{sqldr.colse ();}