C#访问db complex存储过程错误

时间:2013-06-13 20:20:23

标签: c# ms-access dataadapter

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

我正在尝试从访问(OleDb)数据库中检索数据到c#/ wpf DataTable。现在有趣的是:一个简单的查询在访问中工作并下载到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;

QryTestComplex:

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

有什么建议吗?

谢谢,

阿诺德

1 个答案:

答案 0 :(得分:0)

这是你的问题:

cmd.CommandType = CommandType.StoredProcedure;

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