Reader source process (ACCESS ):
Purpose: Use the datareader reading class to read data only forward.
Q: What is datareader?
A: The ADO. Net datareader object can retrieve read-only and only-in data streams from the database. 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 = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("db1.mdb"); // access link string
Oledbconnection connacc = new oledbconnection (strconn); // instantiate the oledb connection class
Connacc. open (); // open the database
String strsql = "select * from table name 1"; // SQL statement
Oledbcommand cmd = new oledbcommand (strsql, connacc); // create a command object
Olddbdatareader DR = cmd. executereader (); // create a datareader object and call executereader to retrieve rows from the data source
Dr. Read (); // start the reader's read method and return each column of the row
//... Data reading ..
Dr. Close (); // close the reader
Connacc. Close (); // close the database
Dataset dataset source process (ACCESS ):
Purpose: use dataset and dataadapter to read data.
Q: What is dataset?
A: The DataSet object is similar to the ADO recordset object, but it is more powerful and has another important difference: DataSet is always disconnected. Dataset objects represent data caches and have similar data structures, such as tables, columns, relationships, and constraints. However, although dataset can run like a database, it is important to remember that the DataSet object does not directly interact with data or other source data. This allows developers to use consistent programming models without worrying about the resident location of the source data.
Q: What is dataadapter?
A: The dataadapter object serves as a bridge between dataset and the data source.
String strconn = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("db1.mdb"); // access link string
Oledbconnection connacc = new oledbconnection (strconn); // instantiate the oledb connection class
Connacc. open (); // open the database
String strsql = "select * from table name 1"; // The SQL statement to be executed
Oledbdataadapter da = new oledbdataadapter (strsql, connacc); // create a dataadapter data adapter instance
Dataset DS = new dataset (); // create a dataset instance
Da. Fill (DS, "Custom virtual table name"); // use the dataadapter fill method (fill) to call the SELECT command
Connacc. Close (); // close the database
Executenonquery source process (ACCESS ):
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 = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("db1.mdb"); // access link string
Oledbconnection connacc = new oledbconnection (strconn); // instantiate the oledb connection class
Connacc. open (); // open the database
String strsql = "insert into table name 1, update table name 1 set, delete from table name 1"; // insert, update, and delete SQL statements
Oledbcommand cmd = new oledbcommand (strsql, connacc); // create a command object
Cmd. executenonquery (); // execute the command
Connacc. Close (); // close the database
Executescalar source process (ACCESS ):
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 = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("db1.mdb"); // access link string
Oledbconnection connacc = new oledbconnection (strconn); // instantiate the oledb connection class
Connacc. open (); // open the database
String strsql = "select count (*) from table name 1"; // SQL statistics, sum, and so on
Oledbcommand cmd = new oledbcommand (strsql, connacc); // create a command object
Int intnum = (INT) cmd. executescalar (); // returns the number of statistics. For sum, double is used.
Connacc. Close (); // close the database
Note: To use SQL Server, change the keyword oledb to SQL, and modify the strconn string to the SQL Server Link parameter.