1
使用C#,我試圖查詢Access數據庫(.accdb)。下面的代碼工作正常,直到它實際嘗試查看包含在字段中的值:Console.WriteLine(rs.Fields.Item(0).Value);)in main()。'adodb.fields'不包含'Item'的定義
我已經搜索上的警告消息:
「ADODB.Fields」不包含關於「項」和沒有擴展方法「項」接受「ADODB.Fields」的第一參數類型的定義可以找到(是否缺少using指令或程序集引用?)
同時,我會搜索「C#ADODB如何引用單個域」,在這些搜索出現 一切都表明,「項目'應該是'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()' – MethodMan 2014-08-27 18:03:52
我會考慮在OleDB中使用這種技術,這裏有一些很好的工作示例,在這個'SO'的答案部分上一篇文章 http:/ /stackoverflow.com/questions/930/how-do-i-connect-to-a-database-and-loop-over-a-recordset-in-c – MethodMan 2014-08-27 18:07:00
誰投了我的問題:你能幫我嗎通過提出如何改善我的問題的建議? – johncroc 2014-08-27 18:08:46