Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I am getting an error when trying to save an XLS to .DBF file.

I am getting an error when trying to save an XLS to .DBF file.

Scheduled Pinned Locked Moved C#
databasecomsecurityperformancehelp
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Glen Childs
    wrote on last edited by
    #1

    Hello, I am getting an error when trying to save my XLS to .DBF. The error is: An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'. I am getting the error at this line of code: var cnn = new OleDbConnection(cnnStr);

    //Connection string for SQL
    var cnnStr = @"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\file.xls;ReadOnly=1;ColNameHeader=False";

            //Creates a new OleDbConnection with an argument of cnnStr
            var cnn = new OleDbConnection(cnnStr);
    
            //creates new datatable in memory to store the read excel spreadsheet.
            var dt = new DataTable();
    
            try
            {
                //Opens the new connection called "cnn". 
                cnn.Open();
    
                string sqlConnectionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True";
    
    
                var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
                //string worksheet = schemaTable.Rows\[worksheetNumber - 1\]\["table\_name"\].ToString().Replace("'", "");
                string sql = String.Format("select \* from \[{0}\]", sqlConnectionString);
                var da = new OleDbDataAdapter(sql, cnn);
                da.Fill(dt);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                // close resources
                cnn.Close();
            }
    

    Any pointers? ;P

    Regards, Glen

    Richard DeemingR 1 Reply Last reply
    0
    • G Glen Childs

      Hello, I am getting an error when trying to save my XLS to .DBF. The error is: An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'. I am getting the error at this line of code: var cnn = new OleDbConnection(cnnStr);

      //Connection string for SQL
      var cnnStr = @"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\file.xls;ReadOnly=1;ColNameHeader=False";

              //Creates a new OleDbConnection with an argument of cnnStr
              var cnn = new OleDbConnection(cnnStr);
      
              //creates new datatable in memory to store the read excel spreadsheet.
              var dt = new DataTable();
      
              try
              {
                  //Opens the new connection called "cnn". 
                  cnn.Open();
      
                  string sqlConnectionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True";
      
      
                  var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                  if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
                  //string worksheet = schemaTable.Rows\[worksheetNumber - 1\]\["table\_name"\].ToString().Replace("'", "");
                  string sql = String.Format("select \* from \[{0}\]", sqlConnectionString);
                  var da = new OleDbDataAdapter(sql, cnn);
                  da.Fill(dt);
              }
              catch (Exception e)
              {
                  throw e;
              }
              finally
              {
                  // close resources
                  cnn.Close();
              }
      

      Any pointers? ;P

      Regards, Glen

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      That looks like an ODBC connection string. Try one of the OLEDB Excel connection strings from: http://www.connectionstrings.com/excel/[^] For example:

      var cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\file.xls;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      G 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        That looks like an ODBC connection string. Try one of the OLEDB Excel connection strings from: http://www.connectionstrings.com/excel/[^] For example:

        var cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\file.xls;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        G Offline
        G Offline
        Glen Childs
        wrote on last edited by
        #3

        Thanks solved that error, I am getting a different error now tho :confused: error: 'DataSource=excelFilePath;InitialCatalog=master;IntegratedSecurity' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

        L J Richard DeemingR 3 Replies Last reply
        0
        • G Glen Childs

          Thanks solved that error, I am getting a different error now tho :confused: error: 'DataSource=excelFilePath;InitialCatalog=master;IntegratedSecurity' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          DataSource is wrong, it should be Data Source: two words.

          Veni, vidi, abiit domum

          G 1 Reply Last reply
          0
          • L Lost User

            DataSource is wrong, it should be Data Source: two words.

            Veni, vidi, abiit domum

            G Offline
            G Offline
            Glen Childs
            wrote on last edited by
            #5

            Okay thanks, same error occurs though with Data Source as two words.

            L 1 Reply Last reply
            0
            • G Glen Childs

              Okay thanks, same error occurs though with Data Source as two words.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The rest of the information in that statement does not look like the example that was suggested to you.

              Veni, vidi, abiit domum

              1 Reply Last reply
              0
              • G Glen Childs

                Thanks solved that error, I am getting a different error now tho :confused: error: 'DataSource=excelFilePath;InitialCatalog=master;IntegratedSecurity' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

                J Offline
                J Offline
                Jason Gleim
                wrote on last edited by
                #7

                Connection strings are provider-specific. The sting you have there uses an Excel file as the "data store" but you are passing SQL-server parameters with InitialCatalog and IntegratedSecurity. Those aren't parameters the "Excel data provider" understands.

                1 Reply Last reply
                0
                • G Glen Childs

                  Thanks solved that error, I am getting a different error now tho :confused: error: 'DataSource=excelFilePath;InitialCatalog=master;IntegratedSecurity' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  You've opened an OLEDB connection to an Excel spreadsheet, and you're now trying to execute the query:

                  select * from [Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True]

                  That's not a valid query. A valid query would look something like:

                  select * from [SheetName$Range]

                  Perhaps if you explain what you're trying to achieve, we might be able to help. :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  G 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    You've opened an OLEDB connection to an Excel spreadsheet, and you're now trying to execute the query:

                    select * from [Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True]

                    That's not a valid query. A valid query would look something like:

                    select * from [SheetName$Range]

                    Perhaps if you explain what you're trying to achieve, we might be able to help. :)


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    G Offline
                    G Offline
                    Glen Childs
                    wrote on last edited by
                    #9

                    I am trying to achieve an application that is able to read in an Excel Spreadsheet (.XLS) and be able to convert/save out to a Database file (.DBF). Regards,

                    Richard DeemingR 1 Reply Last reply
                    0
                    • G Glen Childs

                      I am trying to achieve an application that is able to read in an Excel Spreadsheet (.XLS) and be able to convert/save out to a Database file (.DBF). Regards,

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      Start by reading the sheet:

                      static DataTable LoadExcelSheet(string fileName, int worksheetNumber, bool headers)
                      {
                      if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");
                      if (!File.Exists(fileName)) throw new FileNotFoundException(null, fileName);

                      var cnnStr = string.Format(
                          "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\\"Excel 8.0;HDR={1};IMEX=1\\"", 
                          fileName, 
                          headers ? "Yes" : "No");
                      
                      using (var cnn = new OleDbConnection(cnnStr))
                      {
                          cnn.Open();
                      
                          var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                          
                          // NB: The schema table often contains two rows per sheet;
                          // you need to ignore any which end with "$":
                          var sheetRow = schemaTable.AsEnumerable()
                              .Where(row => !((string)row\["TABLE\_NAME"\]).EndsWith("$"))
                              .Skip(worksheetNumber).FirstOrDefault();
                          
                          if (sheetRow == null) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
                      
                          string sql = string.Format("SELECT \* FROM \[{0}$\]", sheetRow\["TABLE\_NAME"\]);
                          var da = new OleDbDataAdapter(sql, cnn);
                      
                          var dt = new DataTable();
                          da.Fill(dt);
                          return dt;
                      }
                      

                      }

                      That will give you a DataTable containing the data from the Excel sheet. You'll then need to use a SqlConnection and a SqlDataAdapter to update your SQL database.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      G 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        Start by reading the sheet:

                        static DataTable LoadExcelSheet(string fileName, int worksheetNumber, bool headers)
                        {
                        if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");
                        if (!File.Exists(fileName)) throw new FileNotFoundException(null, fileName);

                        var cnnStr = string.Format(
                            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\\"Excel 8.0;HDR={1};IMEX=1\\"", 
                            fileName, 
                            headers ? "Yes" : "No");
                        
                        using (var cnn = new OleDbConnection(cnnStr))
                        {
                            cnn.Open();
                        
                            var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            
                            // NB: The schema table often contains two rows per sheet;
                            // you need to ignore any which end with "$":
                            var sheetRow = schemaTable.AsEnumerable()
                                .Where(row => !((string)row\["TABLE\_NAME"\]).EndsWith("$"))
                                .Skip(worksheetNumber).FirstOrDefault();
                            
                            if (sheetRow == null) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");
                        
                            string sql = string.Format("SELECT \* FROM \[{0}$\]", sheetRow\["TABLE\_NAME"\]);
                            var da = new OleDbDataAdapter(sql, cnn);
                        
                            var dt = new DataTable();
                            da.Fill(dt);
                            return dt;
                        }
                        

                        }

                        That will give you a DataTable containing the data from the Excel sheet. You'll then need to use a SqlConnection and a SqlDataAdapter to update your SQL database.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        G Offline
                        G Offline
                        Glen Childs
                        wrote on last edited by
                        #11

                        Okay Thanks will try that! :-D

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups