OledbConnection 无效参数
OledbConnection Invalid Arguments
我尝试在 Selenium Webdriver C# 中读取 Excel 文件,但在 connection.Open() 处收到无效参数。我哪里错了?
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
namespace LeTuanAnh_Training.TestDataAccess
{
class ExcelDataAccess
{
public static string TestDataFileConnection()
{
var fileName = ConfigurationManager.AppSettings["TestDataSheetPath"];
var con = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = {0}; Extended Properties=Excel 12.0;", fileName);
return con;
}
public static UserData GetTestData(string keyName)
{
using (var connection = new OleDbConnection(TestDataFileConnection()))
{
connection.Open();
var query = string.Format("select * from [Sheet1$] where key='{0}'", keyName);
var value = connection.Query<UserData>(query).FirstOrDefault();
connection.Close();
return value;
}
}
}
}
App.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings configSource="Configurations\Environment.config" />
</configuration>
Environment.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="TestDataSheetPath" value="TestDataAccess/Data.xlsx"/>
</appSettings>
Error Pic
当你在做的时候
using (var connection = new OleDbConnection(TestDataFileConnection()))
Using 关键字打开连接并在 using 块结束时关闭它。所以你不需要 connection.Open() 和 connection.Close().
尝试连接到您的数据库和double-check 数据库连接数据。您的问题与硒无关...
我尝试在 Selenium Webdriver C# 中读取 Excel 文件,但在 connection.Open() 处收到无效参数。我哪里错了?
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
namespace LeTuanAnh_Training.TestDataAccess
{
class ExcelDataAccess
{
public static string TestDataFileConnection()
{
var fileName = ConfigurationManager.AppSettings["TestDataSheetPath"];
var con = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = {0}; Extended Properties=Excel 12.0;", fileName);
return con;
}
public static UserData GetTestData(string keyName)
{
using (var connection = new OleDbConnection(TestDataFileConnection()))
{
connection.Open();
var query = string.Format("select * from [Sheet1$] where key='{0}'", keyName);
var value = connection.Query<UserData>(query).FirstOrDefault();
connection.Close();
return value;
}
}
}
}
App.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings configSource="Configurations\Environment.config" />
</configuration>
Environment.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="TestDataSheetPath" value="TestDataAccess/Data.xlsx"/>
</appSettings>
Error Pic
当你在做的时候
using (var connection = new OleDbConnection(TestDataFileConnection()))
Using 关键字打开连接并在 using 块结束时关闭它。所以你不需要 connection.Open() 和 connection.Close().
尝试连接到您的数据库和double-check 数据库连接数据。您的问题与硒无关...