Open Access document with C#?
I have this class in C# which i developed from examples:
Code:
class ManagedAccess:IDisposable
{
dynamic _access;
dynamic _doc;
bool _isDisposed = false;
public ManagedAccess(string filePath = "")
{
_access = new Microsoft.Office.Interop.Access.Application();
_access.Visible = true;
if (filePath.Equals(string.Empty))
{
_doc = _access.Workbooks.Add();
_doc.Activate();
}
else
{
_doc = _access.Workbooks.Open(filePath);
}
}
protected virtual void Dispose(bool isDisposing)
{
if (this._isDisposed)
return;
if (isDisposing)
{
this._access.Quit();
}
// Release unmanaged resources here
//if (this._excel != null)
//{
// System.Runtime.InteropServices.Marshal.ReleaseComObject(this._excel);
//}
// Indicate that the object has been disposed.
this._isDisposed = true;
}
~ManagedAccess()
{
this.Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
If you check the constructor there is the word "Workbooks" which suits when opening Excel application from C#.
Though Access usage will decrease, we still use old Access software and lot of apps are built with it, so I might need to open an Access program from C#.
What should i replace "Workbooks" with in the following code:
Code:
if (filePath.Equals(string.Empty))
{
_doc = _access.Workbooks.Add();
_doc.Activate();
}
else
{
_doc = _access.Workbooks.Open(filePath);
}
This is using Microsoft office Interop libraries and the IDisposable pattern in C#
Re: Open Access document with C#?
This should help if you need to open an access database from c# code
http://www.vbforums.com/showthread.p...73#post5346573