About SqlCommand and sqlcommand
The novice asp.net should all try the interaction between the front and back as I do! Especially the interaction with databases. The following is my personal experience.
SqlCommand
First, introduce the system. Date. SqlClient namespace.
Note that SqlCommand does not support the DateDrict type.
Create a SqlCommand object
SqlCommand cmd = new SqlCommand (); // create an object
Cmd. Connection = new SqlConnection (connect); // create a Connection string. connect is a Connection string that I have prepared in advance.
Cmd. Connection. Open (); // Open the Connection
Cmd. CommandText = "SELECT [title], [ISBN], [Publisher] FROM [BOOKINFO]"; // create an SQL statement
Cmd. CommandType = CommandType. Text; // specify the Text type.
// SqlCommand does not support TableDriect
// Cmd. CommandText = "BOOKINFO"; // table name
// Cmd. CommandType = CommandType. TableDirect; // specify the table Type
// Cmd. CommandText = "GetBook"; // name of the stored procedure
// Cmd. CommandType = CommandType. StoredProcedure; // specifies that the type is stored procedure.
Table T = new Table (); // used to dynamically receive data
SqlDataReader re = cmd. ExecuteReader (); // create SqlDataReader to read data from the returned value (read from one row)
Try
{
While (re. Read ())
{
TableRow tr = new TableRow (); // create a row tag object
For (int I = 0; I <re. FieldCount; I ++) // obtain the number of columns in each row
{
// Response. Write (re [I]);
TableCell td = new TableCell (); // create a column tag object
Td. Text = re [I]. ToString ();
Tr. Cells. Add (td );
}
// Response. Write ("<br/> ");
T. Rows. Add (tr );
}
This. Controls. Add (t );
}
Finally
{
Re. Close (); // end read
}
Cmd. Connection. Close (); // Close the Connection
}
A small example is complete. There is no style in the table, and the page is ugly. For detailed explanations, refer to MSDN.