Summary of ASP. NET database operation code: SQL Server

Source: Internet
Author: User

ASP. NET database operationsCodeDatareader

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, you can use datareader to improve the applicationProgramPerformance 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 (); // 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.

 

Recommended Editing:

1. Implementation of ASP. NET database Driver Class: dbhelper

2. Three-layer design model for ASP. NET Applications

3. Introduction to the role and structure of ngws runtime in ASP. NET

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.