title: Copy Data from Access to an Existing SQL Server Table
body: <p>This code copies data from access to SQL server table.
  but this code has some problems.</p>
  
  <ol>
  <li>This code can not copy data from access to SQL server table where have data. </li>
  </ol>
  
  <p>My SQL server table has some data and I want to add data from access to under existing data in SQL server table. </p>
  
  <p>How do I add data to the existing table?</p>
  
  <ol start="2">
  <li>Can not read data from access 2007 or 2010.</li>
  </ol>
  
  <p>How do I read data from access 2007/2010</p>
  
  <pre><code>OpenFileDialog openfiledialog1 = new OpenFileDialog();
           openfiledialog1.Title = "select access file";
  
              openfiledialog1.Filter = "Access 2003 (*.mdb)|*.mdb|Access 2007|*.accdb";
              if (openfiledialog1.ShowDialog() == DialogResult.OK)
              {
  
                  string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openfiledialog1.FileName;
                  const string connectionStringDest = @"server=ahmad-pc\anfd;database = phonebook;Integrated Security = true";
                  using (var sourceConnection = new OleDbConnection(connectionString))
                  {
                      sourceConnection.Open();
  
                      var commandSourceData = new OleDbCommand("SELECT * from numberperson", sourceConnection);
                      var reader = commandSourceData.ExecuteReader();
  
                      using (var destinationConnection = new SqlConnection(connectionStringDest))
                      {
                          destinationConnection.Open();
  
                          using (var bulkCopy = new SqlBulkCopy(destinationConnection))
                          {
  
  
                              bulkCopy.ColumnMappings.Add("name", "nameperson"); //THIS A MAPPING REPLACE IT WITH YOUR NEED
                              bulkCopy.ColumnMappings.Add("family", "family1");
                              bulkCopy.DestinationTableName = "profile2";
  
                              try
                              {
                                  bulkCopy.WriteToServer(reader);
                              }
                              catch (Exception ex)
                              {
                                  Console.WriteLine(ex.Message);
                              }
                              finally
                              {
                                  reader.Close();
                              }
                          }
                      }
                      MessageBox.Show("success");
                  }
  
              }
  </code></pre>