C # ADO

Source: Internet
Author: User

ADOAdo. NET is the access structure of the. NET Library, which is the communication bridge between the repository application program and the source, which provides a reference structure for the image, which is used to develop the repository application program. To provide the image, the image is encapsulated by the method of manipulating the repository:
    • . Net Framework Digital Provider
    • DataSet datasets
Ado. NET structures

Five major repositories:

The Connection is used to create a link command to the repository for SQL sentence three common methods: ExecuteNonQuery: Returns the number of rows affected by a change in the number of changes ExecuteReader     : Perform a search and return a SqlDataReader to the image executescalar: Perform a search and return to the first line of the column DataReader used to read the numbers DataAdapter used to fill the bar to fill the Datasetdataset The dataset, which is used in the program to access the library method step ① Create a link to the repository--->② open the library link--->③ create an ADO record set--->④ extract the required numbers from the records--->⑤ the closed record set--->⑥ close link With ADO, you need to reference the System.Data.SqlClient in your program, which contains the number of SQL Server operations
    • SqlConnection Link Repository
    • SqlCommand the name of the image
    • SqlCommandBuilder Generating SQL commands
    • SqlDataReader Digital Reader
    • SqlDataAdapter populating a DataSet with an adapter
    • SqlParameter to save Chengting parameters
    • SqlTransaction the Repository
classProgram {Static voidMain (string[] args) {              //Digital Library Connection string            stringsql ="server =gsdev018hz;database=test; user =gsb6275; pwd=ygt562356610;"; SqlConnection Conn=NewSqlConnection (SQL);//Create a link to an instance of an image, connect to a sourceSqlCommand cmd = conn. CreateCommand ();//The repository operation categoryCmd.commandtext ="Select *from sql_t go delete from sql_t where name = ' Tom '";//compile the numbers in the repositoryConn. Open ();//Open the Repository connection and manipulate the digital//DataReader The readings, Excutereader get the numbers .SqlDataReader dr = cmd. ExecuteReader ();//            intCount =0; if(Dr. HasRows) { while(Dr. Read ()) {count++; stringid = dr["ID"].                    ToString (); stringName = dr["name"].                    ToString (); stringclassname = dr["class"].                    ToString (); stringScore = dr["Fractions"].                    ToString (); Console.WriteLine (ID+"|"+ name+"|"+classname+"|"+score); }} Console.WriteLine ("there are altogether"+ Count +"row Numbers"); // EraseCmd.commandtext ="Delete from sql_t where name = ' Tom ';"; intA = cmd. ExecuteNonQuery ();//ExecuteNonQuery Returns the number of rows affected            if(a>0) {Console.WriteLine ("Delete"+ A +"Line"); }Else{Console.WriteLine ("Delete Failure"); } conn.  Close (); //Accept limited connections, use closed connectionsConsole.ReadLine (); }    }

DataAdapter populating a DataSet with an adapter

classDataAdapter {Static voidMain (string[] args) {DataSet DataSet=NewDataSet (); using(SqlConnection conn =NewSqlConnection ("") {Conn.                Open (); SqlCommand Command=Conn. CreateCommand (); Command.commandtext="Select*from sql_t";//compile the numbers in the repositorySqlDataAdapter dataAdapter =NewSqlDataAdapter (command);//Create SqlDataAdapter and Execute SQLDataAdapter.Fill (DataSet);//Fill Numbers            }        }    }/// <summary>        ///using SqlCommandBuilder to change the numbers/// </summary>        Private Static voidSqlCommandBuilder () {using(SqlConnection conn =NewSqlConnection ("") {Conn.                Open (); SqlCommand Command=Conn.                CreateCommand (); Command.commandtext="Select*from sql_t";//compile the numbers in the repositorySqlDataAdapter da =NewSqlDataAdapter (command); DataSet DS=NewDataSet (); Da.                Fill (DS); SqlCommandBuilder Cmdbuilder=NewSqlCommandBuilder (DA);//the function is to transform the dataset into a SQL sentence to follow the new repositoryDataRow row = ds. tables[0]. NewRow ();//add rows to instantiate a line imagerow[0] =" One"; row[1] =" A"; Ds. tables[0]. Rows.Add (row);//Add to TableDs. tables[0]. rows[1] ="";//Modify the numbersds. tables[0]. rows[2]. Delete ();//Delete a statisticDa. Update (DS);//Update the table and the repository in the dataset.            }        }
DataReader and DataAdapter SqlDataReader:
    • You can only read the library, and all operations must be connected, but to operate on the repository, you can only use SqlCommand.
    • Read only one row in the memory at a time, minus the system.
    • You need to pass through your own read () method to access the specified image.
    • Need to pass through the Close () method to disconnect
SqlDataAdapter:
    • Built on top of the SqlCommand, it has all the functions of the SqlCommand to populate the dataset with an image, and it does not need to be connected to the repository, and can be directly taken from a dataset or DataTable.
    • The fill () method can be used to populate the dataset at once
    • After reading the library, the connection is automatically interrupted.

DataSet DataTable DataRow

The dataset DataSet is a set of independent datasets that are not dependent on the repository, that is, the closed repository can    still be used, and the dataset can store multiple tables (DataTable) in which the DataTable  represents a table of stored memory, stored in the stored table, Before persistence (dataadapter.update) to the repository, a DataTable that is not affected by the library DataRow    is made up of a DataRow group. Datatable.row[i] That means that line I datarowstate    is a very important state in the DataRow including (added add, deleted delete, detached separation, modified modify, Unchange not changed)
Static voidMain (string[] args) {   //Digital Library Connection string            stringsql ="server =gsdev018hz;database=test; user =gsb6275; pwd=ygt562356610;"; //enquiries            stringCommandString ="Select*from sql_t"; //Create SqlDataAdapter and Execute SQLSqlDataAdapter dataAdapter =NewSqlDataAdapter (Commandstring,sql); //Create DataSet DatasetsDataSet DataSet =NewDataSet (); //Add the numbers to the datasetDataAdapter.Fill (DataSet); //Add a list to a set of numbersDataTable datatable = Dataset.tables ("sql_t"); }

C # ADO

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.