How to create a data binding grid with C # and ADO. NET

Source: Internet
Author: User
Tags mdb database

Data access is the basis of any application. In this article, I will explain how to use C # and ADO. NET to access SQL Server-based data and how to display data in a data-bound grid control. I use a simple C # application as an example.

ADO. NETStructure

You do not need to maintain a connection when using ADO. NET. In addition, in ADO. NET, you only need a few lines of code to switch from one data source to another.

Core objects of ADO. NET are:Command, Connection, DataReaderAndDataAdapter. They are the basis for all data operations in. NET.

Core ADO. NETNamespace

  • System. Data: Serves as the basis for other namespaces.DataTable, DataColumn, DataViewAndConstraintsObject.
  • System. Data. Common: Defines common objects shared by various data providers, includingDataAdapter, DataColumnMappingAndDataTableMapping. It is used by the data provider and contains a set used to access the data source.
  • System. Data. OleDb: Defines the objects you use to connect to the data source and modify the data in various data sources. It is written as a common data Provider and executed by the. NET Framework that contains the SQL Server, Microsoft Oracle OLE DB Provider, and Microsoft Jet 4.0 Provider drivers. This namespace is used when you need to connect to many different data sources, and you want to achieve better performance than the provider.
  • System. Data. SqlClient: The SQL Server application interface provides better performance than the common System. Data. OleDb. This is a provider namespace dedicated to SQL Server 7.0 and later versions.
  • System. Data. SqlTypes: Provides classes for SQL Server data types. This namespace is designed for SQL Server and provides better performance than other namespaces, but only applies to the SQL Server backend.
  • System. Data. Odbc: Process all ODBC-compatible drives. Only. NET Framework 1.1 supports this namespace, so you can get it after installing the new Framework.

Data Grid instance

Add a data grid to table dataGrid1, as shown in figure 1. To enable the sample code in list A to run, use the following namespace:

Using System. Data;

Using System. Data. OleDb;

The above Code defines two variables:StrConnAndStrSQL.StrConnUse OLEDB to set the connection string required to use the JET Database and point it to the Northwind. mdb database of the local computer.StrSQLSpecify the query that I want to run on the Access Database (Northwind. mdb.

Next, I defineOleDBDataAdapterObject da and submit it to the query statement (StrSQL) And the connection string (StrConn). Note that I have not created a Connection object in the example.

Then, I define the data group ds, which is used to obtain actual data from the user table (MERS mers) in the grid control. I direct the DataMember feature of the Data Grid Control dataGrid1 to the table where I got the data, and set the DataSource feature of the control to DataSetds. (The DataMember feature obtains/sets a table bound to the control in DataSource. The DataSource feature obtains/sets the data source used to install the control .) When you run the code in list A, result 2 is displayed.

I show the data in the C: DataAccessNorthwind. mdb database. Only the column in the selection statement is displayed. If the selected number of columns or columns exceeds the page size, the grid control automatically displays the scroll bar.

Now you know how to use ADO. NET in the C # application, and how to create a data grid control to display the basic principles of querying returned data.

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.