Working With oledb Adapter

80酷酷网    80kuku.com

   
Submitted ByUser LevelDate of SubmissionMahesh ChandBeginners06/19/2001
Platform Used: VS.NET Beta 2 (Test Build)
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.

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 pbject.
First screen is about the wizard.

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.

Next page provides you options to use SQL Statements or stored procedures.

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

This option let you pick your database tables.

And now you pick what columns you want to get data from the database to the DataAdapter.

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

Last page shows you the activities wizard finished.

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 -

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.

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.

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

Step 4: Attach DataSet with a DataView
Now attach this dataset with a DataView. Drag a DataView control from Toolbox->Data and set it's table property to your DataSet's table.

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.

Now fill the data from DataAdapter to the DataSet by calling DataAdapter's Fill method. I write this code after InitializeComponent();
InitializeComponent();
oleDbDataAdapter1.Fill(dataset11);
Build and run the project and see the result. I see my data in the DataGrid control.




About the Author: Mahesh is admin and founder of C# Corner.  Mahesh has over 5+ years of programming experience in VC++, MFC, VB, SQL Server, ATL/COM, and COM+. Mahesh's background includes Master's in Computer Science and B.Sc. maths. Other achievements are MCP in VC++ 6.0. He presently engaged with Kruse, Inc. Downingtown, PA, US as a software engineer. More details...

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: