C# app.config中的连接字符串
如何在windows窗体应用程序的app.config文件中添加此C#代码连接字符串? 我看到了添加access db的示例,但我需要添加excel文件数据,因此在app.config中找不到有关excel文件连接的上一个问题C# app.config中的连接字符串,c#,excel,C#,Excel,如何在windows窗体应用程序的app.config文件中添加此C#代码连接字符串? 我看到了添加access db的示例,但我需要添加excel文件数据,因此在app.config中找不到有关excel文件连接的上一个问题 OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MISD_report.xls
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MISD_report.xlsx" + @";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
通常他们自己坐在一个区域:
<connectionStrings>
<add name="myConnectionName" providerName="myProvider" connectionString="Data Source=D:\MISD_report.xlsx;Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0" />
</connectionStrings>
我不知道OLE/Excel的providerName是什么,也不知道使用什么名称,因此提供程序无法找到正确的连接字符串。请在web配置文件中尝试此操作-
<connectionStrings>
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0'" />
<add name="ConnStrName" connectionString="Data Source=database;Initial Catalog=database-name;Persist Security Info=True;User ID=your username;Password=your password" />
</connectionStrings>
我自己解决了这个问题
app.config设置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MSIDConn"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MISD_report.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0'"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
此问题以将其添加到app.config的示例为特色。我重新编辑了我的问题。我相信它保持不变,只是
connectionString
属性包含Excel文件。看起来您已经获得了一个有效的连接字符串。链接的问题更多地演示了如何处理提供程序的详细信息。您可以将连接字符串的每一部分存储为和单个键,并从程序中的这些参数构建一个字符串(连接字符串)。我假设这是提供程序名称,但不确定,因为我没有通过ACE OLEi访问excel。我得到的错误为“{”未在ConnectionString中指定OLE DB提供程序。例如,'provider=SQLOLEDB;'。“}”错误是{”未在ConnectionString中指定OLE DB提供程序。例如,'provider=SQLOLEDB;'。“}
private void button1_Click(object sender, EventArgs e)
{
string excelconn = ConfigurationManager.ConnectionStrings["MSIDConn"].ConnectionString;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = excelconn;
OleDbCommand command9 = new OleDbCommand
(
"SELECT P1, P2, P3 " +
" FROM [PE_Actual$] ", conn
);
DataSet ds9 = new DataSet();
OleDbDataAdapter adaptor9 = new OleDbDataAdapter(command9);
adaptor9.Fill(ds9, "testtable");
dataGridView1.DataSource = ds9.Tables[0].DefaultView;
}