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