Blue Theme Orange Theme Green Theme Red Theme
 
Ads by Lake Quincy Media
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ADO.NET & Database » Working with ADO.NET Database Components in VS.NET

Working with ADO.NET Database Components in VS.NET

Working with ADO.NET Database Components in VS.NET and WinForms is so fun. You write few lines of code and these controls take care of rest for you. In this article, I'm going to show you how to use these database Components in your .NET WinForms applications using VB.NET.

Author Rank:
Total page views :  65810
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

Working with ADO.NET Data Components in VS.NET and WinForms is so fun. You write few lines of code and these controls take care of rest for you. In this article, I'm going to show you how to use these Data Components controls in your .NET WinForms applications using VB.NET.

Data-bound Controls

Data-bound controls are WinForms controls those can easily bind with data components. Microsoft Visual Studio.NET is a rich IDE for ADO.NET data components. I'm going to talk about these controls in a moment. In this article, I'm going to talk about three main data-bound controls - DataGrid, ListBox, and a ComboBox.

Data-bound controls have properties, which you can set as a data component and they're ready to present your data in WinForms. DataSource and DisplayMemeber are two important properties.

DataSource property of these controls plays a major role. You can set different kind of data components as datasource property of a control. For example, you can set a DefaultViewManager or a DataView as this property.

Dim ds As DataSet = New DataSet
DataGrid1.DataSource = ds.DefaultViewManager

DisplayMember property can be set to a database table field name if you want to bind a particular field to the control.

' Attach dataset's DefaultView to the datagrid control
Dim dv As DataView = ds.Tables("Employees").DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "FirstName"
 

ADO.NET Data Components in VS.NET

Microsoft Visual Studio.NET provides a rich set of ADO.NET data components. These components sit between WinForms data-bound controls and the data source and passes data back and forth to the controls. These components are:

  • Data Connection
  • DataSet
  • DataView 
  • Data Adapters
  • Data Commands

You can create these components in either at design-time or at run-time. Creating these components at design-time is pretty simple task. You just drag these components on a form and set properties and you're all set.

Connection, data adapter, and command components are specific to a data provider and dataview and dataset are common components.

ADO.NET Data Providers

In Microsoft .NET Beta 2, ADO.NET has three types of data providers. Each data provider is designed to work with different types of data sources. All of these data providers provide same classes for a connection, data adapter and command classes to work with and work in similar fashion.

These data providers are:

SQL Data Providers

SQL data providers are designed to work with SQL Server 70 or later databases. The connection, command and data adapter classes are - SqlConnection, SqlCommand, and SqlDataAdapter.

OLE DB Data Providers

Ole-db data providers are designed to work with any OLE-DB data source. You need to have an OLE-DB provider to work with a data source. The connection, command and data adapter classes are - OleDbConnection, OleDbCommand, and OleDbDataAdapter.

ODBC Data Providers

ODBC data providers is a recent addition to the .NET SDK. This API doesn't ship with .NET Beta 2. You need to download it separately than .NET SDK. You can download it Microsoft's site at ODBC SDK. ODBC providers are designed to work with any ODBC data source. You need to have an ODBC driver to work with a data source. The connection, command and data adapter classes are - ODBCConnection, ODBCCommand, and ODBCDataAdapter.

As I mentioned earlier, working with all of these data providers is similar accept the class names and data sources. So if you know one of them, you can just replace data source and the class names.

Working with Data Components

There are few simple steps include to work with data components. Just follow these steps one by one.

Step 1: Connect to a data source

First step is to create a connection to the data source. You use a Connection object to connect to a data source. You neat to create a connection string and create connection object. Here I'm using MS-Access 2000 as my data source and OleDB Data Adapters to work with the data source.

' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\\northwind.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)

Step 2: Creating a Data Adapter

Now you create a data adapter. A data adapter constructor takes two arguments - A SQL string and a connection object.

Dim sqlStr As String = "SELECT * FROM Employees"
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
 

Step 3: Creating and Filling a DataSet

Now next step is to create a dataset and fill it by using data adapter's Fill method.

' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees") 
 

Step 4: Bind to a data-bound control

The last step is to bind the data set to a data-bound control using above discussed methods.

' Attach dataset's DefaultView to the datagrid control
DataGrid1.DataSource = ds.DefaultViewManager 
          

Sample Application

This sample application is a Windows application which three controls - a DataGrid, a ListBox, and a ComboBox and three buttons Fill DataGrid, Fill ListBox, and Fill ComboBox respectively.

When you click on these buttons, the fill the data from the data source to the control. The code is shown in the below table -

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\\northwind.mdb"
Dim sqlStr As String = "SELECT * FROM Employees"
' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(conStr)
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees")
' Attach dataset's DefaultView to the datagrid control
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
Private
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\\northwind.mdb"
Dim sqlStr As String = "SELECT * FROM Employees"
' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(conStr)
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees")
' Attach dataset's DefaultView to the datagrid control
Dim dv As DataView = ds.Tables("Employees").DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "FirstName"
End Sub
Private
Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=c:\\northwind.mdb"
Dim sqlStr As String = "SELECT * FROM Employees"
' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(conStr)
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees")
' Attach dataset's DefaultView to the datagrid control
Dim dv As DataView = ds.Tables("Employees").DefaultView
ComboBox1.DataSource = dv
ComboBox1.DisplayMember = "FirstName"
End Sub
 

References:

  1. MSDN Magazine Articles 
  2. MSDN Library Samples and Documentation


Login to add your contents and source code to this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of 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 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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
Become a Sponsor
 Comments
VB.Net by Roshan On October 19, 2007
Dear Sir, I wrote codes correctly, but the error msg comming from the following line dataadep.Fill(ds, "Customers") Herewith my total codes set and please help me to go fowared. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim conStr As String = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=D:\Roshan\VBNet\Test.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1" Dim sqlstr As String = "Select * from Employees" Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(conStr) conn.Open() Dim dataadep As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlstr, conn) Dim ds As DataSet = New DataSet dataadep.Fill(ds, "Customers") DataGrid1.DataSource = ds.DefaultViewManager conn.Close() End Sub
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.