About SqlCommand and sqlcommand

Source: Internet
Author: User

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.

 

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.