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.
- StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
-
- SqlConnection ConnSql =NewSqlConnection (strConn );
-
- ConnSql. Open ();// Open the database
-
- StringStrSQL ="SELECT * FROM table name 1"; SqlCommand cmd =NewSqlCommand (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.
- StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
-
- SqlConnection ConnSql =NewSqlConnection (strConn );
-
- ConnSql. Open ();StringStrSQL ="SELECT * FROM table name 1";
-
- 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.
- StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
-
- SqlConnection ConnSql =NewSqlConnection (strConn );
-
- 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.
- StringStrConn ="Uid = Account; pwd = password; database = database; server = server";
-
- SqlConnection ConnSql =NewSqlConnection (strConn );
-
- ConnSql. Open ();// Open the database
-
- 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.