ARTICLE

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

Posted by Mayur Dighe Articles | ADO.NET in VB.NET April 23, 2011
The Article shows the Transferring of data from one type of Data Source to another type of Data Source.
Download Files:
 
Reader Level:

The Article shows the Transferring of data from one type of Data Source to another type of Data Source.

Aim: 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:

  • Three DataGridView Controls to display data.
  • 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
     

  2. 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"];
    }

     

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

    TransData1.gif

    Figure 1

    TransData2.gif

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

    TransData3.gif

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

    TransData4.gif

    Figure 4 : Transferring a row from SQL Server Data Source to MS-Access Data Source
     
  4. 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
  5. 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.

Login to add your contents and source code to this article
share this article :
post comment
 

Useful Article

Posted by Dinesh Beniwal Apr 23, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor