0

I have the following code :

ADOX.Catalog cat = new ADOX.Catalog();
string pathToNewAccessDatabase = "Data Source=D:\\Data\\NewMDB.mdb";

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + pathToNewAccessDatabase + ";Jet OLEDB:Engine Type=5");

System.Data.OleDb.OleDbConnection AccessConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + pathToNewAccessDatabase);

AccessConn.Open();
System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand("SELECT * INTO [ReportFile] FROM [Data Source=server_path; Initial Catalog=database_name; User Id=user_name; Password=pass_word;Trusted_Connection=False].[dbo.DataSourceTable]", AccessConn);

AccessCommand.ExecuteNonQuery();
AccessConn.Close();

I want to select from SQL SERVER into the ACCESS database.

Also, if the password contains the [ character, how do I escape that ?

2
  • 1
    I is quite possible to refer to either the SQL Server or the Access database in-line and update one to the other with a single sql statement : stackoverflow.com/questions/3473380/… Commented Jan 18, 2012 at 20:04
  • @Remou Yes, you are correct, but there are changes I have to make in SqlServer in ordr for that to work. I am a little scared I might mess up something. (like performance) Commented Jan 23, 2012 at 8:48

1 Answer 1

1

I suggest establishing an SQLConnection first to the SQL server and query your desired data into a DataTable.

using (SqlConnection conn = new SqlConnection("yourConnectionString"))
{
    using (SqlCommand comm = new SqlCommand("Select columns from targetTable", conn))
    {                    
        SqlDataReader reader = comm.ExecuteReader();
        DataTable tbl = new DataTable();
        tbl.Load(reader);
    }
}

After you have the data in your datatatable, create the query for your insert command from it by looping through the data.

string insertCommandString = string.Empty;

for (int row = 0; row < tbl.Rows.Count; row++)
{
    insertCommandString = "Insert into yourTableName(yourColumnNames) values(";
    for (int column = 0; column < tbl.Columns.Count; column++)
    {
        if(tbl.Columns[column].DataType == typeof(String))
        {
            insertCommandString += "'" + tbl.Rows[row][column].ToString() + "'";
        }
        else
        {
            insertCommandString += tbl.Rows[row][column].ToString();
        }

        if (column < tbl.Columns.Count - 1)
        {
            insertCommandString += ",";
        }
   }

   insertCommandString += ")";

   System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand(insertCommandString, AccessConn);
   AccessCommand.ExecuteNonQuery();
 }

You should not escape any character from password.

Sign up to request clarification or add additional context in comments.

Comments

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.