ASP. NET database: four typical types of SQL Server code

Source: Internet
Author: User

There are many types of ASP. NET database operation code. Today, let's take a look at four typical types of SQL Server code.

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.

 
 
  1. StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
  2.  
  3. SqlConnection ConnSql =NewSqlConnection (strConn );
  4.  
  5. ConnSql. Open ();// Open the database 
  6.  
  7. StringStrSQL ="SELECT * FROM table name 1"; SqlCommand cmd =NewSqlCommand (strSQL, ConnSql); SqlDataReader dr = cmd. ExecuteReader (); Read ();
  8.  
  9. Dr. Close ();// Close the database 

DataSet for ASP. NET database operation code

Purpose: DataSet and DataAdapter read data.

 
 
  1. StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
  2.  
  3. SqlConnection ConnSql =NewSqlConnection (strConn );
  4.  
  5. ConnSql. Open ();StringStrSQL ="SELECT * FROM table name 1";
  6.  
  7. SqlDataAdapter da =NewSqlDataAdapter (strSQL, ConnSql); DataSet ds =NewDataSet (); 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.

 
 
  1. StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
  2.  
  3. SqlConnection ConnSql =NewSqlConnection (strConn );
  4.  
  5. ConnSql. Open ();StringStrSQL ="Insert into table name 1, UPDATE table name 1 SET, delete from table name 1"; SqlCommand cmd =NewSqlCommand (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.

 
 
  1. StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
  2.  
  3. SqlConnection ConnSql =NewSqlConnection (strConn );
  4.  
  5. ConnSql. Open ();// Open the database 
  6.  
  7. StringStrSQL ="Select count (*) FROM table name 1"; SqlCommand cmd =NewSqlCommand (strSQL, ConnSql );IntIntNum = (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.

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.