Opt In (Do Not Edit Here)

May 31, 2011

Data Transfer Through Different Data Sources


Transfer Data from and to MS-Access, SQL Server, and XML Data Sources



Introduction:
            Generally, Transferring Data From one Type of Data Source to another and vice-versa is required when an application may have to deal with different the data sources at a time.
            The article contains the simplest way to achieve the mentioned aim.


Requirements:
n  Three DataGridView Controls to display data.
n  Six Command Buttons to Perform associated actions


Explanation:
  1. First we have to make some declaration as follows.
                       
#region "Declaration"
                      //For Sql COnnection
SqlConnection SqlCon = new SqlConnection("Integrated Security=True;Initial Catalog=SqlDB;Data Source=.\\SqlExpress");
                      SqlCommand SqlCmd;

                      //For OleDb Connection
OleDbConnection OleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;   Data Source=..\\..\\AccessDB.mdb;");
          OleDbCommand OleCmd;

        //For All DataSources
                    DataSet Dset = new DataSet();   //For XML DataSource
        DataSet Dset1 = new DataSet();  //For OleDb DataSource
        DataSet Dset2 = new DataSet();  //For SQL DataSource
                  DataRow row;
        #endregion







  1. After Declaration, Now we make a Procedure named LoadData () to initialize the data from different databases and to display it into respective DataGridView control.

    public void LoadData()
    {
           
            //Load the data from MS-Access file to the dataGridView1
  OleDbDataAdapter OleAdapt =
new OleDbDataAdapter("Select * from User_Info Order By   UserID", OleCon);
            OleAdapt.Fill(Dset1, "User_Info");
            dataGridView1.DataSource = Dset1.Tables["User_Info"];


            //Load the data from SQL file to the dataGridView2
            SqlDataAdapter SqlAdapt =
new SqlDataAdapter("Select * from User_Info Order By UserID", SqlCon);
            SqlAdapt.Fill(Dset2, "User_Info");
            dataGridView2.DataSource = Dset2.Tables["User_Info"];

//Load the data from XML file to the dataGridView3
            Dset.ReadXml("..\\..\\XMLData.xml");
            dataGridView3.DataSource = Dset.Tables["UserInfo"];
      
}



  1. Figure 1 shows the data into respective DataGridView at form load


Figure 1

Figure 2 : Transferring a selected row from MS-Access Data Source to XML Data Source

























Figure 3 : Transferring a row from XML Data Source to SQL Server Data Source


     
Figure 4 : Transferring a row from SQL Server Data Source to MS-Access Data Source

  
  1. To copy a row, first select the row from source DataGridView and click the respective button.
§  Figure 2 shows copying data from MS-Access to XML Data Source
§  Figure 3 shows copying data from XML to SQL Server Data Source
§  Figure 4 shows copying data from SQL Server to MS-Access Data Source

  1. We can also do the other operations mentioned in the program similarly as shown in Figure 1/2/3.



Summary:
            In this article, we see the transferring of data from one data source to another in C# Programming. We can also perform other operations on mentioned title such as Moving, Deleting or Inserting the data of single record or may of bulk records also.










About The Author :

Freelancer and IT Engineer
Softwares Developed by Mayur Dighe ImmortalDotNet.WordPress.com

0 comments :

All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion

Top