C # basic steps for database development using ado.net

Source: Internet
Author: User
1. Create a connection object to connect to the database.
2. Configure the DataAdapter object and create and operate the DataSet.
3. Add tables in the database to DataSet.
4. Help set the DataSet to the DataGrid. Use the Fill method of DataAdapter to Fill the data into DataSet, and the data in the database is displayed in the DataGrid on the user interface.

In c #, floating point numbers cannot accurately express many real numbers. They only represent approximate values.
To solve this problem, c # provides a special data type decimal, which can be precise to 28 digits after the decimal point. But the range is not as big as double.
Char type: 65535 characters.

Method classification for querying records from databases in c #: c # Development and Research
Two methods are generally used:

One is direct access through the DataReader object, and the other is access through the Dataset and Dataadapter object.

You can use the Datareader object of ADO. NET to retrieve data from the database. The retrieved data forms a read-only data stream, which is stored in the client's network buffer. The read method of the Datareader object can forward to the following record. By default, each read method is executed to store only one record in the memory, with very little overhead.

Before creating a datareader, you must first create a sqlcommand object, and then call the executereader method of the object to construct the sqldatareader object, instead of directly using the constructor.

The following example program accesses the sqlserver database, reads records from the northwind data using datareader, and outputs the query results through the console.

Program code

Code

Using System;

Using System. Data;

Using System. Data. SqlClient;

Namespace ReadDataFromDB {

Class Class1 {

Static void Main (string [] args ){

String myconn = "Data Source = localhost; Integrated Security = SSPI; Initial Catalog = Northwind ";

// SQL statement to be executed

String mysql = "select orderID, CustomerID from orders where CustomerID = 'chops '";

// Open the database connection.
SqlConnection myconnection = new SqlConnection (myconn );
Myconnection. Open ();

// Create a SqlCommand object
SqlCommand mycommand = new (mysql, myconnection );

// Construct the DataReader object using the ExecuteReader () method of SqlCommand.
SqlDataReader myreader = mycommand. ExecuteReader ();

While (myreader. read ()){

Console. WriteLine (myreader. GetInt32 (0) + "," + myreader. GetString (1 ));

}

Myreader. Close ();

Myconnection. Close ();

}

}

}

 

Another way to read records from a database is to use the Dataset object and Dataadapter object. Dataset is one of the main components of ADO. NET,

It is used to cache the data information retrieved from the data source. As the bridge between Dataset and the data source, Dataadapter is used to retrieve and save data.

After the Dataadapter obtains data from the database, it uses the Fill method to Fill the data in the Dataset. The following uses Sqldataadapter as an example to describe how to use the Dataset object and Dataadapter object to read records from the database. The key steps for executing a query are as follows:

1. Create a Sqlconnection to establish a connection with the database and pass the connection string.

2. Construct the Sqldataadapter object containing the query statement;

3. To use the query result to Fill in the Dataset object, call the Fill command.

Example:

Program code

Code

Using System;

Using System. Data;

Using System. SqlClient;

Namespace ReadDataFromDB2 {

Class Class2 {

Static void Main (string [] args ){

  

String myconn = "Data Source = localhost; Integrated Security = SSPI; Initial Catalog = Northwind ";

// Initialize SQL statement.
String mysql = "select CompanyName, ContactName from MERs ";

// Initialize connection string.
SqlConnection myconnection = new SqlConnection (myconn );

// Execute SQL.
SqlDataAdapter mydataadapter = new SqlCommand (mysql, myconnection );

Myconnection. Open ();

DataSet ds = new DataSet ();

Mydataadapter. Fill (ds, "MERs ");

For (int I = 0; I <ds. Tables ["MERs"]. Rows. Count; I ++ ){

Console. writeLine (ds. tables ["MERs"]. row [I] [0]. toString () + "," + ds. table ["customers"]. row [I] [1]. toString ());

 

Retrieve the value of a row in the DataGrid
DataGrid. Item (I). cells [j]. text;

DataGridView1.DataSource = dataset. Tables ["company"]. Defaultvies;
Equivalent to the following two statements:
DataGridView1.DataSource = dataset;
DataGridView1.Datamumber = "company ";

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.