Summary of ASP. NET database operation code: SQL Server

Source: Internet
Author: User

DataReader for ASP. NET database operation code

Function: DataReader reading class, which reads data only forward.

Read-only and inbound data streams. Because there is only one row of data in the memory each time, DataReader can improve application performance and reduce system overhead. It also provides an unbuffered data stream that enables the process logic to effectively process the results returned from the data source in order. Because the data is not cached in the memory, DataReader is an appropriate choice when retrieving a large amount of data.

String strConn = "uid = Account; pwd = password; database = database; server = server ";

SqlConnection ConnSql = new SqlConnection (strConn );

ConnSql. Open (); // Open the database

String strSQL = "SELECT * FROM table name 1"; SqlCommand cmd = new SqlCommand (strSQL, ConnSql); SqlDataReader dr = cmd. ExecuteReader (); Read ();

Dr. Close (); // Close the database

DataSet for ASP. NET database operation code

Purpose: DataSet and DataAdapter read data.

String strConn = "uid = Account; pwd = password; database = database; server = server ";

SqlConnection ConnSql = new SqlConnection (strConn );

ConnSql. Open (); string strSQL = "SELECT * FROM table name 1 ";

SqlDataAdapter da = new SqlDataAdapter (strSQL, ConnSql); DataSet ds = new DataSet (); Fill (ds, "Custom virtual table name"); Close (); // Close the database

ExecuteNonQuery for ASP. NET database operation code

Purpose: ExecuteNonQuery is used to insert, update, and delete data.

Q: What is ExecuteNonQuery?

A: In ADO. NET, the ExecuteNonQuery method is used to execute commands that do not need to return results, such as insert, delete, and update operations.

String strConn = "uid = Account; pwd = password; database = database; server = server ";

SqlConnection ConnSql = new SqlConnection (strConn );

ConnSql. open (); string strSQL = "insert into table name 1, UPDATE table name 1 SET, delete from table name 1"; SqlCommand cmd = new SqlCommand (strSQL, ConnSql ); executeNonQuery (); Close (); // Close the database

ExecuteScalar for ASP. NET database operation code

Purpose: Use ExecuteScalar statistics.

Q: What is ExecuteScalar?

A: The ExecuteScalar method returns an aggregate function of a single value, such as sum and total number of rows.

String strConn = "uid = Account; pwd = password; database = database; server = server ";

SqlConnection ConnSql = new SqlConnection (strConn );

ConnSql. Open (); // Open the database

String strSQL = "select count (*) FROM table name 1"; SqlCommand cmd = new SqlCommand (strSQL, ConnSql); int intNum = (int) cmd. executeScalar (); Close (); // Close the database

The above is an introduction to the code used for database operations between asp.net and SQL Server. I hope it will be helpful to you.

  1. Differences between Bind and Eval in ASP. NET: two types of binding
  2. Simple common ASP. NET Code 2)
  3. Simple common ASP. NET code 1)
  4. ASP. NET tips: Use of unmanaged COM components
  5. Another implementation of concatenating strings in ASP. NET: Response. Write

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.