0

有什么帮助我在这里做错了吗?如果我试图读取示例 TestCaseName 的唯一行,case_1那么我将获取不同行的数据。

我怎样才能确保它只阅读被要求阅读的内容?我正在使用 where 子句,但似乎没有过滤它。

string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", EXCELFILENAME);

string testCaseName = "case_1
string query = String.Format("SELECT * from [{0}$] WHERE TestCaseName=\"{1}\"", workbookName, testCaseName);

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);  
DataTable myTable = dataSet.Tables[0]; 

TestCaseName       Name    Active   Status  etc...
----------------------------------------------------------------
case_1             Tom     yes      Completed   etc...
----------------------------------------------------------------
case_2             John    yes                  etc...
----------------------------------------------------------------
case_3             Jim     yes                  etc...
----------------------------------------------------------------
case_4             Don     yes                  etc...
----------------------------------------------------------------
case_5             Sam     yes      Visitor     etc...
----------------------------------------------------------------
4

1 回答 1

0

这是我测试的代码。它似乎完美地工作。如果您不这样做,那么我只能假设您的电子表格的结构与我的不完全相同。

string connectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
string testCaseName = "case_1";
string query = "SELECT * from [Sheet1$] WHERE TestCaseName=\"" + testCaseName + "\"";
DataTable dt = new DataTable();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
    conn.Open();

    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, conn))
    {
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        dt = ds.Tables[0];
    }

    conn.Close();
}
于 2012-10-31T01:29:21.423 回答