0

寻找这个奇怪问题的提示。

我正在尝试将访问(OleDb)数据库中的数据检索到 ac#/wpf DataTable 中。现在有趣的是:一个简单的查询在访问中工作并被下载到数据表中,就好了。一个(稍微)更复杂的查询在 Access 中有效,但在 c# 中出错。

这是代码:

 OleDbConnection conn = new OleDbConnection("myconn");
 DataTable dt = new DataTable();
 OleDbCommand cmd = new OleDbCommand();
 OleDbDataAdapter adapter = new OleDbDataAdapter();

 conn.Open();

       //3 flavors          
       //string n = "QryTestComplex";        This gives an error in c#, works in access
       //string n = "SELECT * FROM CURSIST"; This works ( with cmd.CommandType =  
                                                                   etc, commented out)
       //string n = "QryTestSimple";         This works   ( SQL = same as above )

cmd = new OleDbCommand(n,conn);
cmd.CommandType = CommandType.StoredProcedure;

adapter.SelectCommand = cmd
            try
            {
                adapter.Fill(dt);
                int t = 0;
                foreach (DataRow dr in dt.Rows)
                { t++; Console.WriteLine(t + dr[0].ToString()); }
            }
            catch (Exception exc)
            {
            Console.WriteLine("{0} Exception caught.", exc);
            }

            conn.Close();

查询是:

QryTestSimple:

SELECT * FROM Cursist;

QryTest 复杂:

SELECT 
    Module.Status, 
    ModuleData.Datum, 
    LeerActiviteit.Naam, 
    Module.Locatie, 
    Module.Onderwijskundige, 
    Module.Organisator, 
    Module.Coordinator, 
    Module.Stoplicht, 
    Module.ID, Cluster.Naam AS CNaam, 
    (
        SELECT '(dag' & count(M.ID) + 1 & ')'  
        FROM  
            Module AS M  
            INNER JOIN ModuleData AS MD 
                ON M.ID = MD.Module_ID 
        WHERE Module.ID = M.ID 
            AND MD.Datum <ModuleData.Datum  
        HAVING  
            (
                SELECT count(P.ID) 
                FROM 
                    Module AS P 
                    INNER JOIN 
                    ModuleData AS PD 
                        ON P.ID = PD.Module_ID  
                WHERE Module.ID= P.ID 
            ) >1 
    ) AS DagNummer
FROM 
    (
        LeerActiviteit 
        INNER JOIN 
        (
            [Module] 
            LEFT JOIN 
            Cluster 
                ON Module.Onderdeel_Van_Cluster_ID = Cluster.ID
        ) 
            ON LeerActiviteit.ID = Module.LeerAct_ID
    ) 
    INNER JOIN 
    ModuleData 
        ON Module.ID = ModuleData.Module_ID
WHERE (((ModuleData.Datum)>Now()));

复杂查询在访问中有效,但是在 C# 中调试时,我得到:

System.Data.OleDb.OleDbException (0x80004005):IErrorInfo.GetDescription 失败,出现 E_FAIL(0x80004005)。

休息时间adapter.Fill(dt);

有什么建议么?

谢谢,

阿诺德

4

1 回答 1

1

这是你的问题:

cmd.CommandType = CommandType.StoredProcedure;

Access 中没有这样的东西。如果您按原样阅读查询,请将其设为 .Table。如果您正在编写一个 select 语句,请将其设为 .Text。

于 2013-06-13T20:43:41.877 回答