c# - 'adodb.fields' 不包含 'Item' 的定义

标签 c# field adodb recordset

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

我已经搜索了警告信息:

'ADODB.Fields' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument type of 'ADODB.Fields' could be found (are you missing a using directive or an assembly reference?)

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

我有“使用 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();
        }
    }
}

最佳答案

据我所知,您正在尝试错误地访问字段名称。尝试这样的事情

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

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

关于c# - 'adodb.fields' 不包含 'Item' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533938/

相关文章:

c# - while循环用数组填充datagridview

jQuery - 检测隐藏输入字段的值变化

sql - 查找我的数据库的 DSN?

c# - 在 DocumentDB 中查询满足特定条件的文档计数的最佳方法是什么?

c# - 如何在 C# 项目中使用 Clang?

c# - asp.net 复选框事件未触发

linq - Field扩展方法的要点

php - 如何从单个字段中查找超时和超时并将其值提取到 SQL 中的另一个表

mysql - ADOdb.connection.execute "create table"通过 MSAccess 与 Mysql 后端问题

excel - 对象_connection的方法打开失败