Oracle 5.0 Reference Manual page 1881

Table of Contents

Advertisement

daCountry.Fill(dsCountry, "Country");
Note the
method is a
Fill
connection with the database and retrieve the required data, and then populates the Data Set when the
method is called. The second parameter "Country" is the table in the Data Set to update.
Fill
Updating the Data Set
The data in the Data Set can now be manipulated by the application as required. At some
point, changes to data will need to be written back to the database. This is achieved through a
method, the
MySqlDataAdapter
daCountry.Update(dsCountry, "Country");
Again, the Data Set and the table within the Data Set to update are specified.
Working Example
The interactions between the DataSet,
can be a little confusing, so their operation can perhaps be best illustrated by working code.
In this example, data from the World database is read into a Data Grid View control. Here, the data
can be viewed and changed before clicking an update button. The update button then activates code
to write changes back to the database. The code uses the principles explained above. The application
was built using the Microsoft Visual Studio to place and create the user interface controls, but the main
code that uses the key classes described above is shown below, and is portable.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
MySqlDataAdapter daCountry;
DataSet dsCountry;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT Code, Name, HeadOfState FROM Country WHERE Continent='North Americ
daCountry = new MySqlDataAdapter (sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daCountry);
dsCountry = new DataSet();
daCountry.Fill(dsCountry, "Country");
dataGridView1.DataSource = dsCountry;
dataGridView1.DataMember = "Country";
}
Connector/Net Tutorials
method, the Data Adapter knows how to establish a
MySqlDataAdapter
method.
Update
MySqlDataAdapter
1861
and
MySqlCommandBuilder
classes

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents