1
 public void SetConnection(string text1, string text2, string text3, string text4, string text5, string text6, string text7)
    {


        connectionString1 = "Initial Catalog=testdb; Data Source=work\\sqlexpress";




        database = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Users\\test.xls';Extended Properties= \"Excel 8.0;HDR=Yes;IMEX=1\";");
        database.Open();
        database1 = new OleDbConnection("Provider=SQLOLEDB.1;" + connectionString1);
        database1.Open();
    }

data1 = DataAccess.DatabaseTables("SELECT * from [Sheet1$])", DataAccess.database); 

 public DataTable DatabaseTables(string QueryString, OleDbConnection DataConnection)
    {

        OleDbCommand SQLQuery = new OleDbCommand();
        DataTable data = null;
        SQLQuery.Connection = null;
        OleDbDataAdapter dataAdapter = null;
        SQLQuery.CommandText = QueryString;
        SQLQuery.Connection = DataConnection;
        data = new DataTable();
        dataAdapter = new OleDbDataAdapter(SQLQuery);

        dataAdapter.Fill(data);
        return data;
    }

对于从 data1 开始的代码行,我收到异常“from 子句中的语法错误”。此行调用它下面的函数 DatabaseTables 生成此异常。我还在 fn SetConnection 中包含了使用 jet 等的连接字符串。我认为问题可能出在连接字符串本身。目前我必须打开 excel 文件以防止出现“无法解密”错误。我确实尝试了 ACE 驱动程序,但出现了错误。excel 工作簿保存为 97-03 工作簿。

4

1 回答 1

1

改变

data1 = DataAccess.DatabaseTables("SELECT * from [Sheet1$])", DataAccess.database); 

data1 = DataAccess.DatabaseTables("SELECT * from [Sheet1$]", DataAccess.database);

有一个额外的右括号。

希望它有效。

于 2013-05-20T10:11:05.320 回答