Sponsored Ad

Sunday, September 27, 2009

C# Dataset

One of the abundant appearance alien by microsoft in the .net technology is the dataset . The dataset is the article agnate the the acceptable ADO recordset . However the dataset has abounding cogent differences.
  • The dataSet can hold the results of many SQL queries .
  • You can use the dataset while the connection is closed .
  • You can create a dataset from an XML file .
  • You can write XML directly from a dataset .

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 ds1 = new DataSet();
            DataSet ds2 = new DataSet();
            DataTable dt ;
            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(ds1, "First Table");
                oledbAdapter.SelectCommand.CommandText = secondSql;
                oledbAdapter.Fill(ds2, "Second Table");
                oledbAdapter.Dispose();
                connection.Close();

                ds1.Tables[0].Merge(ds2.Tables[0]);
                dt = ds1.Tables[0];

                for (i = 0; i <= dt.Rows.Count - 1; i++)  
                {              
                    MessageBox.Show(dt.Rows[i].ItemArray[0] + " -- " + dt.Rows[i].ItemArray[0]);

                }        
            }          
            catch (Exception ex)  
            {            
                MessageBox.Show("Can not open connection ! ");  
            }      
        }
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers