Database steps in program design __ Database

Source: Internet
Author: User

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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.