Database connection use--connect to the database using ADO

Source: Internet
Author: User

First, ADO. NET Introduction

Ado. NET is a class library of many classes that provide many objects to perform operations such as connection, query, insert, UPDATE, and delete of data. The main objects include the following:

1. Connection object: Used to connect to the database (OLE DB using Oledbconnection,sql server using SqlConnection)

2. Command object: Used to execute SQL commands against the database, such as inserting, deleting, modifying, querying

3. DataReader object: Used to return read-only data from the database

4. DataAdapter object: Used in conjunction with DataSet object to realize control of database

5. DataSet object: Can be considered as an in-memory database

Second, the method of reading the database from ADO

Ado. NET provides a class library (specifically for accessing SQL Server databases)

The namespaces that are imported in the code-behind file are:

Usying System.Data;

Usying System.Data.SqlClient;

Third, the use of the core object method

(i) SqlConnection object

Step one Create connection object and set connection string

SqlConnection conn=new SqlConnection ("Data sourse= ...; Initial catalog=.; ")

Step two open the database connection using the Open method

Conn. Open ();

Step three operations Database

Step four close the database connection

Conn. Close ();

II) SqlCommand object

Step one create the SqlCommand object and insert the data

SqlCommand cmd= new SqlCommand ();

String sql= "Insert into......values (...)";

Step two execute the command by method

int

< P align= "left" method name

return type

executenonquery

Executes SQL and returns the number of rows affected

executescalar

object

Executes SQL and returns the first row of the first column of data

executereader

sqldatareader

Returns a read-only data flow object

Database connection use--connect to the database using 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.