Create a data-bound grid with C # and Ado.net

Source: Internet
Author: User
Tags define contains mdb database connect odbc ole query access database
ado| Data data access is the foundation of any application. In this article, I'll show you how to access SQL Server based data in C # and Ado.net, and how to display data in a data-bound grid control. I use a simple C # application for example.

   ADO. NET Structure

There is no need to maintain a connection using ado.net. In addition, in ado.net, you can go from one data source to another with just a few lines of code.

Ado. NET's core objects are command, Connection, DataReader and DataAdapter. They are. The basis of all data operations in net.

   Core Ado.net Namespaces

System.Data: Is the basis of other namespaces and complements the DataTable, DataColumn, DataView, and constraints objects.

System.Data.Common: Defines common objects that are shared by various data providers, including DataAdapter, DataColumnMapping, and DataTableMapping. It is used by the data provider and contains a collection for accessing the data source.

System.Data.OleDb: Defines the objects you use to connect to a data source and modify the data in a variety of data sources. It is written as a regular data provider and is executed by the. NET framework that contains SQL Server, Microsoft Oracle OLE DB provider, and Microsoft Jet 4.0 provider drives. 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: Use the SQL Server application interface directly to provide better performance than the more common System.Data.OleDb. This is a provider namespace specifically built for SQL Server 7.0 and above.

System.Data.SqlTypes: Provides classes specifically for the data types of SQL Server. This namespace is designed for SQL Server and provides better performance than other namespaces, but applies only to SQL Server backend.

SYSTEM.DATA.ODBC: Handles all compatible ODBC drives. Only the. NET Framework 1.1 supports this namespace, so you can get it by installing a new version of the framework.

   Data Grid Instance

Add a data grid to the table DataGrid1, as shown in Figure 1. In order for the sample code in List A to run, you need to take advantage of the following namespaces:


Using System.Data;

Using System.Data.OleDb;

List A

Using System.Data;
Using System.Data.OleDb;

private void Form1_Load (object sender, System.EventArgs e)
{
String strconn, strSQL;
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data source=" + "C:\\dataaccess\\northwind.mdb"; strSQL = "Select CustomerID, CompanyName, ContactName, ContactTitle," ;
strSQL = strSQL + "address, city, Country from Customers";
OleDbDataAdapter da = Newoledbdataadapter (strSQL, strconn);
DataSet ds = NewDataSet ();
Da. Fill (ds, "Customers");
DataGrid1.DataMember = "Customers";
Datagrid1.datasource=ds;
}

The code above defines two variables: strconn and strSQL. Strconn uses OLE DB to set the connection string required to take advantage of the Jet database and point to the Northwind.mdb database location of the local computer. STRSQL Specifies the query that I want to run on an Access database (Northwind.mdb).

Next, I define the OleDbDataAdapter object Da and submit it to the query statement (STRSQL) and the connection string (strconn). Note that I did not establish a connection (Connection) object in the example.

Then I define the Data group DS, which is used to get the actual data from the user table (Customers) in the grid control. I dataGrid1 the DataMember attribute of the data grid control to the table where I got the data and set the control datasource characteristics to Datasetds. (DataMember feature gets/sets the table of a bound control in DataSource, DataSource feature gets/sets the data source used to install the control.) When you run the code in List A, the result is as shown in Figure 2.


I display the data in the C:\\dataaccess\\northwind.mdb database and see only the columns I selected in the selection statement. If you select a column or columns that exceed the size of the page, the grid control automatically displays scroll bars.

Now you know the basics of using ado.net in a C # application and creating a data grid control that displays the query's return 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.