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.
- Differences between Bind and Eval in ASP. NET: two types of binding
- Simple common ASP. NET Code 2)
- Simple common ASP. NET code 1)
- ASP. NET tips: Use of unmanaged COM components
- Another implementation of concatenating strings in ASP. NET: Response. Write