简体   繁体   English

OledbConnection 无效参数

[英]OledbConnection Invalid Arguments

I try to read Excel file in Selenium Webdriver C# but got Invalid Arguments at connection.Open()<\/strong> .我尝试在 Selenium Webdriver C# 中读取 Excel 文件,但在connection.Open()<\/strong>处得到 Invalid Arguments。 What am I wrong?我错了什么?

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;
            }
        }
    }
}

When you are doing当你在做

using (var connection = new OleDbConnection(TestDataFileConnection()))

The Using keyword open the connection and closes it when the using block ends. Using 关键字打开连接并在 using 块结束时关闭它。 So you don't need the connection.Open() and the connection.Close().所以你不需要connection.Open()和connection.Close()。

Try to connect to your DB and double-check the DB connection data.尝试连接到您的数据库并仔细检查数据库连接数据。 Your question has nothing to do with Selenium...你的问题与硒无关......

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

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