';adodb.fields';不包含';项目';

本文关键字:项目 包含 adodb fields | 更新日期: 2023-09-27 18:19:55

使用C#,我试图查询Access数据库(.accdb)。下面的代码运行良好,直到它到达实际尝试查看字段中包含的值的行:Console.WriteLine(rs.Fields.Item(0).value);)在main()中。

我已经搜索了警告信息:

"ADODB.Fields"不包含"Item"的定义,也找不到接受第一个参数类型"ADODB.Fields"的扩展方法"Item"(是否缺少using指令或程序集引用?)

此外,我还搜索了"C#ADODB如何引用单个字段"这些搜索中出现的所有内容都表明"Item"应该是"ADODB.Recordset.Fields"命名空间的成员;或者向我展示如何使用迭代器对当前记录中的每个字段进行迭代,这不是我想要的。

我的代码中有"using ADODB;"指令,以及"ADODB"引用:(我的声誉水平不允许我发布我准备的屏幕剪辑,所以我想你必须相信我的话。)

如何引用Fields集合中的单个字段?(最好用名字,但我也可以用索引)

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ADODB;
using iTextSharp;

namespace HistAssessPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void cmdCreatePDFs_Click(object sender, EventArgs e)
        {
            main();
        }
        private void main()
        {
            // throw new NotImplementedException();
            string strConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                "Data Source='C:''MyLocalDirectory''MyAccessdb.accdb';" + 
                                "Persist Security Info=False;"; 
            Connection db = new Connection();
            db.Open(strConnStr);
            Recordset rs = new Recordset();
            rs.ActiveConnection = db;
            rs.CursorType = CursorTypeEnum.adOpenForwardOnly;
            rs.LockType = LockTypeEnum.adLockReadOnly;
            rs.Open("select LAST_NAME from ClientTbl;");
            while (!rs.EOF)
            {
                Console.WriteLine(rs.Fields.Item("LAST_NAME").Value); //also tried rs.Fields.Item(0).Value
                rs.MoveNext();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void Form1_FormClosing(object sender, EventArgs e)
        {
            // Code in here to clean things up (sever connection to db, destroy objects, etc.) before actually exiting the app
            MessageBox.Show("I'm about to close.");
        }
        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

';adodb.fields';不包含';项目';

从我所看到的情况来看,您试图错误地访问字段名。试试这个

rs.fields["LAST_NAME"].ToString(); //this should do the trick 

rs.fields["LAST_NAME"].Value; should work as well