Connection database related operation of ASP dynamic website construction

Source: Internet
Author: User

To connect to a database:

String str = @ "server= server name; Integrated security=sspi;database= database name;";

Note encapsulating the common class, extracting the frequently repeated parts and increasing the efficiency of the Code.

Establishing a database Connection object

SqlConnection conn = new SqlConnection (str);
Conn. Open ();
SqlCommand cmd = new SqlCommand (SQL, conn);
Cmd. ExecuteNonQuery ();
Conn. Close ();

There are two arguments to the command object's constructor, one for the SQL statement to execute, and the other for the database connection object. After you create the command object, you can execute the SQL command.

Cmd. ExecuteNonQuery (); The return value is the number of rows affected, the int type, which returns 1 when used for select;

Query two methods

SqlConnection conn = new SqlConnection (str);
DataTable dt = new DataTable ();
Conn. Open ();
SqlDataAdapter da = new SqlDataAdapter (SQL, conn);
Da. Fill (DT);
Conn. Close ();

SqlConnection conn = new SqlConnection (str);
DataSet ds = new DataSet ();
Conn. Open ();
SqlDataAdapter da = new SqlDataAdapter (SQL, conn);
Da. Fill (DS);
Conn. Close ();

Datasets and DataTable are inclusive relationships, the former contains multiple latter, commonly used DataTable, querying multiple tables generally with a DataTable.

DataSet first table second row the third column of data:

Ds. Tables[0]. ROWS[1][2]. ToString ();
The first row of the DataTable the second column of data:
Dt. ROWS[0][1]. ToString ();

Connection database related operation of ASP dynamic website construction

Related Article

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.