Add, delete, modify, and query in ADO. NET

Source: Internet
Author: User

Add, delete, modify, and query in ADO. NET

ADO. NET:
Data Access Technology

Is a link connecting C # And MSSQL

You can use ADO. NET to write temporary data in the memory to the database.
You can also extract data from the database to the memory for the program to call.

Basic of all data access technologies

Basic Format of database connection:
Two classes are required.
1. SqlConnection for database connection
2. Database Operation SqlCommand


// 1. Connect to the database
// When writing a connection string, you should immediately think of four points to finish writing. 1. Which server to connect to, 2. Which database to connect to, 3. connection username, 4. Password
String SQL = "server =.; database = Data0720; user = sa; pwd = 123;"; // write the connection string
// Instantiate the data connection class and write the connection string to the constructor. After this class is constructed, it is connected to the specified server and database.
SqlConnection conn = new SqlConnection (SQL );

// 2. Set the operations on the tables in the database
// Create an operation class for the database through the connected database
SqlCommand cmd = conn. CreateCommand ();

// Compile a TSQL statement
Cmd. CommandText = "delete from Users where UserName = '" + Uname + "'";

// 3. execute the operation
Conn. Open (); // Open the database connection
Cmd. ExecuteNonQuery (); // database operation execution
Conn. Close (); // The database connection is closed.

Add, delete, and modify:
SqlConnection
SqlCommand
Cmd. ExecuteNonQuery ();

Query:
SqlConnection
SqlCommand
SqlDataReader
Cmd. ExecuteReader ()

Basic Query format:
// Database connection class (connection string)
SqlConnection conn = new SqlConnection ("server = ..; database = Data0720; user = sa; pwd = 123 ");
// Database operation class, constructed through the above connection class
SqlCommand cmd = conn. CreateCommand ();
// Query statement
Cmd. CommandText = "select * from Users ";

Conn. Open (); // database connection Enabled
SqlDataReader dr = cmd. ExecuteReader (); // call this method to query !!!!

// Every time this method is executed, the pointer goes down to read the data in the following row. If there is no data below, a false value is returned.
While (dr. Read ())
{
// If the data is read, the row of data read is put in the dr object. You can extract the data in two ways.
// 1. Use index-dr [index value]
Console. WriteLine (dr [0] + "" + dr [1] + "" + dr [2] + "" + (bool) dr [3])? "Male": "female") + "" + Convert. toDateTime (dr [4]). toString ("MM dd, yyyy") + "" + dr [5]);
// 2. Use the column name-dr ["column name"]
Console. WriteLine (dr ["UserName"] + "" + dr ["Nation"]);
}
Conn. Close (); // The database connection is closed.

// The data stored in the database may not be directly presented to the user. Therefore, the C # segment will be displayed after the data is processed.

 

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.