简体   繁体   English

web.config中的oledb C#

[英]oledb in web.config c#

I'm trying to use excel sheet as my database. 我正在尝试使用excel工作表作为数据库。 I'm converting it into a datatable. 我正在将其转换为数据表。 When I try to pass the connectionstring in my method it populates with an exception: 当我尝试在我的方法中传递连接字符串时,它会填充异常:

Input string was not in correct format. 输入的字符串格式不正确。

And I found that error is in my Web.Config file. 而且我发现该错误在我的Web.Config文件中。 I used the following config: 我使用以下配置:

<connectionStrings>
  <add name ="Excel07ConString" 
       connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;Driver={Microsoft Excel Driver(*.xlsx)};DBQ=xlsx HDR={1}'"
       providerName="System.Data.OleDb"/>
</connectionStrings>

You are using placeholders in the connection string, such as Data Source={0} . 您正在连接字符串中使用占位符,例如Data Source={0} You have to fill in the appropriate values in those placeholders, for instance Data Source='C:\\Excel Sheets\\MyFile.xslx' . 您必须在这些占位符中填写适当的值,例如Data Source='C:\\Excel Sheets\\MyFile.xslx'

You can try following connection string: 您可以尝试以下连接字符串:

    <connectionStrings>
       <add name ="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0; data source=&quotD:\Desky\Excel_to_Datatable\Excel_to_Datatable\Files\DB in Excel.xlsx&quot; Extended Properties=&quotExcel 12.0;IMEX=1&quot;" providerName="System.Data.OleDb"/> 

You need to substitute some values in place of {0}(data source) and {1}(HDR). 您需要替换一些值来代替{0}(数据源)和{1}(HDR)。

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=<data source here>;
Extended Properties='Excel 12.0 Xml;Driver={Microsoft Excel 
Driver(*.xlsx)};DBQ=xlsx HDR=<Yes/No>

"HDR=Yes;" “ HDR =是;” indicates that the first row contains columnnames, not data. 表示第一行包含列名,而不是数据。 "HDR=No;" “ HDR =否;” indicates the opposite. 表示相反。

"Data Source" Indicates source file path. “数据源”指示源文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM