0

I have a MS Access file with a Number table. Also I have a SQL Server database with a profile table.

How do I import data from the number table into the profile table?

2
  • do you want to convert your ms access database to sql server(mdf) db file? Commented Apr 8, 2013 at 15:08
  • no , i want add info to exist table. Commented Apr 8, 2013 at 15:15

1 Answer 1

4

It will be something like this:

    const string connectionString = "YOUR ACCESS CONNECTION STRING";
    const string connectionStringDest = "YOUR SQL SERVER CONNECTION STRING";
    using (var sourceConnection =
          new OleDbConnection(connectionString))
    {
        sourceConnection.Open();

        var commandSourceData = new OleDbCommand(
            "SELECT COL1, COL2 FROM TABLE_X;", sourceConnection);
        var reader =
            commandSourceData.ExecuteReader();

        using (var destinationConnection =
                   new SqlConnection(connectionStringDest))
        {
            destinationConnection.Open();

            using (var bulkCopy =
                       new SqlBulkCopy(destinationConnection))
            {
                bulkCopy.DestinationTableName =
                    "dbo.TABLE_DEST";

                try
                {
                    bulkCopy.WriteToServer(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    reader.Close();
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

error is : Syntax error in FROM clause. this error yellow `var reader = commandSourceData.ExecuteReader();
I do not know your table structure so i put an example one
"SELECT COL1, COL2 FROM TABLE_X;" to "SELECT COL1, COL2 FROM TABLE_X" semicalon is ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.