C#和DAO。为什么RS.Fields(“ColName”)不起作用?

时间:2009-09-10 22:25:52

标签: c# .net dao

    dao.DBEngine DBE;
    dao.Database DB;
    dao.Recordset RS;
    string DBPath;

        DBPath = "C:\\Test\\test.mdb";
        DBE = new dao.DBEngine();
        DB = DBE.OpenDatabase(DBPath, false, false, "");
        RS = DB.OpenRecordset("SELECT * FROM TEST", dao.RecordsetTypeEnum.dbOpenSnapshot, 0, dao.LockTypeEnum.dbOptimistic);
        RS.MoveLast(0);
        RS.MoveFirst();
        String Val = RS.Fields("ColName");

最后的说明适用于VBA或VB.NET我想,为什么它不能用于C#?

2 个答案:

答案 0 :(得分:3)

使用方括号,显式声明属性名称,在本例中为Value,并将其强制转换为字符串:


string Val = (string)RS.Fields["ColName"].Value;

答案 1 :(得分:2)

尝试String Val = RS.Fields [“ColName”]; C#使用方括号来引用集合