Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Visual Studio .NET » Working With OleDb Data Adapters

Working With OleDb Data Adapters

This step by step tutorial guides you to create your database applications using OleDb Data Adapters in VS.NET.

Author Rank :
Page Views : 10427
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Description of the Article:

Microsoft Visual Studio .NET (VS.NET afterwards) is full with Data-bound controls and wizards. These controls and wizards help you to generate your applications in no time. You just set some properties of these controls and use wizards to generate your code. In couple of minutes, you develop a full-fledged working application.

In this series of articles, my first article is OleDb Data Adapter Controls in VS.NET. In this article, I'll show you how to display data in a DataGrid control by writing only one line of code. You just follow these simple steps:

Step 1: Create Project

Pick Visual C#->Windows Application project and type your project name and pick a directory. See Figure 1.

 

Figure 1.

Step 2: Add OleDb Data Adapter

Drag an OleDbDataAdapter control from Toolbox->Data to your form. Data Adapter Configuration Wizard appears and it guides you towards to create your Data Adapter object.

First screen is about the wizard. See Figure 2.

Figure 2.

Second screen will let you create a connection. If you already have connections, they are available in a drop down list. You either pick a connection from your list or create a new connection. New Connection button will launch Data Link Properties Dialog, which lets you create a new connection. I'm using Northwind database (Microsoft Access 2000) comes with Office 2002. See Figure 3.

Figure 3.

Next page provides you options to use SQL Statements or stored procedures. For Microsoft Access databases, only "Use SQL statements" option is available. See Figure 4.

Figure 4.

Next step is to generate SQL statement. You use Query Builder button to build a query. See Figure 5.

Figure 5.

This option let you pick your database tables. I pick Employees table. The Add button adds the table to the listing. You can even pick more than one tables. See Figure 6.

Figure 6.

And now you pick what columns you want to get data from the database to the DataAdapter. I pick some columns Address, City, EmployeeID, FirstName, LastName and Title. See Figure 7.

Figure 7.

This page shows you your SQL Statement. You can even write SQL statement by yourself (with no help of Query Builder). See Figure 8.

Figure 8.

Last page shows you the activities wizard finished. See Figure 9.

Figure 9.

Step 3: Generate DataSet

Next step is to generate DataSet and connect DataSet to a DataView component. You generate a DataSet by using "Generate DataSet" option in DataAdapter's Properties dialog. See Figure 10.

Figure 10.

The Generate DataSet option is also available from the right click on a data adapter. See Figure 11. You can also call DataAdapter Configuration Wizard from here if you wish to change your data source or want to make any changes using Configure Data Adapter option. See Figure 11.

Figure 11.

Generate DataSet link shows you dataset. Pick "New" option and type your dataset name. You can pick as many as tables you want from the list. I keep default option. See Figure 12.

Figure 12.

By clicking "OK" button, wizard generates a dataset and shows you it's properties dialog. You can change DataSet name and other properties. "View Schema" option shows you the XML Schema for this dataset added by the wizard and "DataSet Properties" option shows you the properties. See Figure 13.

Figure 13.

This action adds one DataSet derived class to your project. We're not going in to details of this class. See Figure 14.

Figure 14.

Step 4: Attach DataSet with a DataView

Now we're going to view data in a DataGrid control. We'll use recently generated dataset to fill a DataGrid control.

Now attach this dataset with a DataView. To attach a dataset to a DataView, first you need to add a DataView control to the form. Drag a DataView control from Toolbox->Data and set its table property to your DataSet's table. See Figure 15.

Figure 15.

Now you can bind this DataView with any data-bound controls.

Step 5: Attach DataView with the DataGrid Control and Fill the DataSet

Now drag a DataGrid control from Toolbox->WinFroms to the form and set its DataSource property to DataView. See Figure 16.

Figure 16.

Last step is one and only one line of code to add. 

Now fill the data from DataAdapter to the DataSet by calling DataAdapter's Fill method. I write this code after InitializeComponent(); See BOLD like in the following listing.

InitializeComponent();
oleDbDataAdapter1.Fill(dataSet11);

Build and run the project and see the result. I see my data in the DataGrid control. See Figure 17.

Figure 17.

Conclusion

You just saw how VS.NET provides tools, which you can use to write database applications merely writing few lines of code.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.