Practical Tip: How to create a database connection using Visual Studio 2003?
Note |
---|
Versions of MindFusion.Charting 3.2 and higher do not support design-time data binding in Visual Studio.NET 2003. |
In order to use any data from a database you should first make a connection to the database (1), select the data you need (2) and transform the data in such format that it can be used in applications (3). Here is how to do this in Visual Studio.NET 2003:
1. Make connection to the database
- Select the Data tab in the Toolbox pane in design mode in Visual Studio.NET.
- Select the type of connection you want - OleDbConnection or SqlConnection and drag the tab chosen onto the form. The Connection control appears in the bottom of the design window.
- In the properties palette of the connection choose ConnectionString and select New from the drop down combo box.
- Follow the steps of the connection wizard in order to create connection to the database you want.
2. Select the data you need
- With the DataConnection already done, drag OleDbDataAdapter or SqlDataAdapter from the Data tab in the Toolbox pane. While the control is being dragged a DataAdapter wizard starts.
- Follow the wizard in order to create a database query and select the necessary fields and tables from the database.
3. Transform the data in a way it can be used in a application
- With DataAdapter ready choose Create DataSet from the hyperlinks in the bottom of the DataAdapter properties browser.
- The newly created DataSet appears next to the connection and adapter controls. Now select it in the property browser of the chart as value of the DataSource Property. Also select a table as value of DataMember. Fill the other data-related properties with the names of the columns which you wish the chart control to read automatically.
- Now the DataAdapter should be filled with the data from the DataSet. Add the following code anywhere in your project, a form_load event handler is a good choice:
C#
Copy Code
|
---|
OleDbDataAdapter1.Fill( dataSet1 ); chart1.DataBind(); |
Notes: |
---|
- Connection refers to the created SqlConnection or OleDbConnection object;
- DataAdapter refers to the created SqlDataAdapter or OldDbDataAdapter;
- chart1 refers to the instance of the Chart - derived class created - line, area, bar, radar;
- dataSet1 refers to the newly created instance of the DataSet class;
- OleDbDataAdapter1 refers to the newly created instance of the OleDbDataAdapter class. The same code counts when you use SqlDataAdapter.
|
See Also
Using VisualStudio 2005