ADO. NET 5-delete a record using the Command object --- ShinePans,

Source: Internet
Author: User

ADO. NET 5-delete a record using the Command object --- ShinePans,

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 = connectio N; try {SC. Open (); // Open the database connection Console. WriteLine ("the database connection has been enabled! "); 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 ("deleted successfully! "); // 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 (@ name, @ age, @ address, @ grade )"; // Add a parameter and assign it to cmd. parameters. add ("@ name", SqlDbType. varChar, 10 ). value = "pan"; cmd. parameters. add ("@ age", SqlDbTyp E. 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 code for adding the 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 ();}}}

Delete execution:




Status after deletion:





Conclusion: Pay attention to the correctness of plain text.


Describes the five main classes in ADONET and their functions

Connection object
The Connection object is mainly used to enable the Connection between the program and the database. You cannot obtain data from the database if you do not use the linked object to open the database. This object is at the bottom layer of ADO. NET. We can generate this object by ourselves or automatically generate it by other objects.

Command object
The Command object can be used to send commands to the database, such as sending query, addition, modification, and deletion commands to the database, as well as calling the stored programs in the database. This object is structured on the Connection object, that is, the Command object is linked to the data source through

DataAdapter object
The DataSetCommand object is used to transmit data between data sources and DataSet. It can issue commands through the Command object and put the obtained data into the DataSet object. This object is structured on the Command object and provides many functions used with DataSet. In Beta 2, The DataSetCommand object is renamed as DataAdapter.

DataSet object
The DataSet object can be regarded as a Cache, which can keep the data queried from the database and even display the whole database. DataSet can not only store multiple tables, but also obtain data Table structures such as primary keys through the DataSetCommand object and record the association between data tables. The DataSet object can be called ADO. NET heavyweight object, which is structured on the DataSetCommand object and does not have the ability to communicate with the data source itself. That is to say, we use the DataSetCommand object as a DataSet object and a bridge between data sources for data transmission.

DataReader object
You can use the DataReader object to read data in sequence instead of other operations. The DataReader object only reads data from the data source in a descending order, and the data is read-only and does not allow other operations. Because DataReader only reads one row at a time and can only be read-only, it not only saves resources but also improves efficiency. In addition to high efficiency, DataReader can reduce network load because it does not need to transmit all data.
Reference: hi.baidu.com/ksfangdao

List the five main objects in ADONET and briefly describe them.

Connection object
Command object, indicating the command to be executed and the stored procedure!
Datareader is a forward read-only data stream.
Dataadapter is a powerful Companion tool that supports addition, deletion, modification, and query.
Dataset is a data-level object, equivalent to one or more tables in the memory!

Related Article

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.