c# Access数据库复杂存储过程错误

本文关键字:存储过程 错误 复杂 数据库 Access | 更新日期: 2023-09-27 18:18:22

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

我试图从访问(OleDb)数据库检索数据到c#/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;

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

Complex查询在access中工作,但在c#中调试时,我得到:

System.Data.OleDb。OleDbException (0x80004005): IErrorInfo.日志含义GetDescription失败与E_FAIL (0 x80004005)。

adapter.Fill(dt);

有什么建议吗?

谢谢,阿诺德

c# Access数据库复杂存储过程错误

你的问题是:

cmd.CommandType = CommandType.StoredProcedure;

Access里可没有这样的东西。如果您按原样读取查询,请将其命名为. table。如果你正在编写一个select语句,请使用。text .