Print -
C# Dataset with multiple tables - OLEDB
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
OleDbConnection connection ;
OleDbDataAdapter oledbAdapter ;
DataSet ds = new DataSet();
string firstSql = null;
string secondSql = null;
int i = 0;
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
firstSql = "Your First SQL Statement Here";
secondSql = "Your Second SQL Statement Here";
connection = new OleDbConnection(connetionString);
try
{
connection.Open();
oledbAdapter = new OleDbDataAdapter(firstSql, connection);
oledbAdapter.Fill(ds, "First Table");
oledbAdapter.SelectCommand.CommandText = secondSql;
oledbAdapter.Fill(ds, "Second Table");
oledbAdapter.Dispose();
connection.Close();
//retrieve first table data
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
}
//retrieve second table data
for (i = 0; i <= ds.Tables[1].Rows.Count - 1; i++)
{
MessageBox.Show(ds.Tables[1].Rows[i].ItemArray[0] + " -- " + ds.Tables[1].Rows[i].ItemArray[1]);
}
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}