【问题标题】:'adodb.fields' does not contain a definition for 'Item'“adodb.fields”不包含“项目”的定义
【发布时间】:2014-10-21 10:31:32
【问题描述】:

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

我已经搜索过警告信息:

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

我也搜索过“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()
  • 我会考虑将这种技术与 OleDB 一起使用,这里有一些很好的工作示例,位于此 SO 的答案部分中 以前的帖子 stackoverflow.com/questions/930/…
  • 致对我的问题投了反对票的人:您能帮我就如何改进我的问题提出建议吗?
  • @DJKRAZE - 就是这样!太感谢了。如果您愿意将您的解决方案作为答案(而不是评论)放入,我会将其标记为答案,以便您获得荣誉。
  • 糟糕。我说得太早了。添加“.Value”而不是“.ToString()”,返回字段的实际内容。太感谢了。如果您愿意将您的解决方案作为答案(而不是评论)放入,我会将其标记为答案,以便您获得荣誉。

标签: c# field adodb recordset


【解决方案1】:

您试图从我所看到的内容中错误地访问字段名称。试试这样的

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    相关资源
    最近更新 更多