ADO. NET-data reader, DataReader --- ShinePans,
Depending on different data providers, DataReader can be divided into four categories: SqlDataReader, OleDbDataReader, OlbeDataReader and OracleDataReader
A clever analogy: if the database is a reservoir, SqlConnection is the water cage, SqlCommand is the water pumping machine, SqlDataReader is the water pipe, SqlDataReader can only read one record at a time, each time SqlDataReader calls the Read method, it will get a record from the database, and the Read method will return a False value. You can use the Wihle loop to call the Read method of SqlDataReader to Read records in the database, sqlDataReader means that the connection to the database should be maintained during database reading. If the connection is closed at this time, Data Reading will fail.
The SqlCommand object calls the ExecuteScalar method to query the number of records in the table. The SqlCommand object calls the ExecuteDataReader method to query all records in the table.
Source code:
Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using System. threading. tasks; namespace SQLTest {class Program {static void Main (string [] args) {// connect to the database string connection = "server = Pan Shang \ SQLEXPRESS; database = db_test; trusted_Connection = true "; SqlConnection SC = new SqlConnection (connection); // SC. connectionString = conne Ction; try {SC. Open (); // Open the database connection Console. WriteLine ("the database connection has been enabled! "); SqlCommand cmd = new SqlCommand (" SELECT * FROM db_student ", SC); SqlDataReader sdr = cmd. executeReader (); // execute the query record command while (sdr. read () {Console. writeLine ("{0} {1} {2} {3}", sdr [0], sdr [1], sdr [2], sdr [3]);} // START: 4. query database records ///////////////////////////////////// /// // * SqlCommand cmd = new SqlCommand ("SELECT count (*) FROM db_student ", SC); int I = (int) cmd. executeScalar (); // Execute Command Console for searching records. writeLine ("A total of {0} pieces of data in the table", I. toString (); * // END: 4. query database records ///////////////////////////////////// /// // START: 3. modify the database data code /////////////////////////////////// /// // * SqlCommand cmd = new SqlCommand ("UPDATE db_student SET student_grade = 99 where student_name = @ name ", SC); // create the SqlCommand object cmd. parameters. add ("@ name", SqlDbType. varChar ). value = "pan"; int I = cmd. ExecuteNonQuery (); if (I> 0) Console. WriteLine ("modified successfully! "); * // END: 3. modify the database data code /////////////////////////////////// /// // START: 1. delete the database record code segment //////////////////////////////////// //// // * string plain text = "delete from db_student WHERE student_name = @ name "; sqlCommand cmd = new SqlCommand (plain text, SC); cmd. parameters. add ("@ name", SqlDbType. varChar ). value = "pan"; int I = cmd. executeNonQuery (); if (I> 0) Console. writeLine ("the record is deleted successfully.! "); * // END: 1. delete the database record code segment //////////////////////////////////// /// // START: 2. add the recorded code //////////////////////////////////// //// // * SqlCommand cmd = new SqlCommand (); // create the SqlCommand object cmd. commandType = CommandType. text; // set the command to run the command cmd. connection = SC; // set the object property cmd. commandText = "insert into db_student (student_name, student_age, student_address, student_grade) VALUES (@ nam E, @ age, @ address, @ grade) "; // Add a parameter and assign the cmd value to the parameter. parameters. add ("@ name", SqlDbType. varChar, 10 ). value = "pan"; cmd. parameters. add ("@ age", SqlDbType. int ). value = 19; cmd. parameters. add ("@ address", SqlDbType. varChar ). value = "Wuhan"; cmd. parameters. add ("@ grade", SqlDbType. int ). value = 100; int I = cmd. executeNonQuery (); // execute the database add record command if (I> 0) Console. writeLine ("successfully added the record"); * // The console outputs the Add record // END: 2. code for adding a record /////// //////////////////////////////////////// /// //} Catch (Exception ex) {Console. writeLine ("Opening Database Error: {0}", ex. message);} finally {SC. close (); Console. writeLine ("the database connection is closed! ") ;}System. Console. ReadLine ();}}}
Running result:
ADONET emergency
System; -- contains the. net most basic class
System. ComponentModel; -- provides a class used to implement runtime and design-time behavior of components and controls.
System. Data; -- Data Access
System. web; -- provides the class and interface System that enables the browser to communicate with the server. web. sessionState; -- provides classes and interfaces for storing data specific to a single client in a Web application on the server.
System. Web. UI; WEB User Interface Class
System. Web. UI. WebControls; -- WEB control set
System. Web. UI. HtmlControls; -- HTML control set
Reference: MSDN
C # How does the background program use ADOnet to read data from EXCEL to DataSet?
PublicDataSet ImportExcel (string strFileName) // The parameter is the file path + file name. DataSource.
{
If (strFileName! = "")
{
String conn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + strFileName + "; Extended Properties = Excel 8.0 ";
String SQL = "select * from [Sheet1 $]";
OleDbDataAdapter da = newOleDbDataAdapter (SQL, conn );
DataSet ds = newDataSet ();
Try
{
Da. Fill (ds, "datatable ");
}
Catch
{
}
Return ds;
}
Else
{
Return null;
}
}