1

My intent is to copy excel data into SQL Server Compact Edition.

I have read the difference between SQL Server Compact and SQL Server from here but couldn't find it out.

Also, know that constructor of SqlBulkCopy takes parameter of SqlConnection and not SqlCeConnection.

Here is my code:

public void importDataFromExcel(string excelFilePath)
{
        string sSQLTable = "Inflations";
        string myExcelDataQuery = "Select CUSIP,FM,TNT,CPI_INDEX from [Sheet1$]";
        string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
        string sSqlConnectionString = "Data Source=C:\\Users\\abc\\Documents\\datafile.sdf;Password=12345;";

        OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
        OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
        OleDbConn.Open();
        OleDbDataReader dr = OleDbCmd.ExecuteReader();

        SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
        bulkCopy.DestinationTableName = sSQLTable;

        while (dr.Read())
       {
            bulkCopy.WriteToServer(dr);
        }
        OleDbConn.Close();
} 
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
jaczjill
  • 334
  • 9
  • 25

2 Answers2

2

You can use my SqlCeBulkCopy library, available via NuGet for SQL CE 4.0, or otherwise available here: https://sqlcebulkcopy.codeplex.com/

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
0

I think this thread is related to your problem. You may also look at library, proposed for using also in that conversation.

Community
  • 1
  • 1
Mr Zak
  • 361
  • 2
  • 10