应该可以在 .net 代码中使用 dao recorset 只需添加对 dao 的引用:在 VS 中打开“解决方案资源管理器”>“引用”>“添加引用”>“.Net”,您应该会看到组件“dao”。
例如 C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\dao.dll。
访问 VBA 代码:
Option Compare Database
Option Explicit
Public Sub Test()
Dim db As DAO.Database
Dim rec As DAO.Recordset
Set db = Application.CurrentDb
Set rec = db.OpenRecordset("Table1")
Dim dotNetObject As New COMAccessibleTest.ReadData
dotNetObject.GetData rec
rec.Clone
db.Close
End Sub
C#代码:
using System.Runtime.InteropServices;
using System.Text;
namespace ReadDataTest
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("772de12c-3ade-4f43-9981-fd3c39691520")]
public interface IReadData
{
void GetData(ref dao.Recordset data);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("2acf2296-63b0-47b2-ab77-51c4e35e7365")]
[ProgId("ReadDataTest")]
public class ReadData : IReadData
{
public void GetData(ref dao.Recordset data)
{
var lines = new StringBuilder();
data.MoveFirst();
while (!data.EOF)
{
lines.AppendLine(data.Fields[0].Value + "," + data.Fields[1].Value + "," + data.Fields[2].Value + "," + data.Fields[3].Value);
data.MoveNext();
}
var file = new System.IO.StreamWriter("c:\\Temp\\test.txt");
file.WriteLine(lines);
file.Close();
}
}
}