Question How to execute access database function?

sams2017

New member
Joined
Feb 7, 2018
Messages
2
Programming Experience
1-3
Hi all,

I have joined today , I am beginner to .net.

Importing the csv file to access database using import spec through code , any help

I have written function in access database (.mdb file)

open access database-database tools-visual basic-created the module (Module name Load)

Sub Import_CSV()
Dim strFile As String 'Filename
DoCmd.TransferText acImportDelimi, OPS_Import_Specs, _
"Raw Data", strPath & strFileList(intFile), -1
End Sub

How to execute this function in c# , so that the c# function will return the success or failure.

Thanks in advance.
 
I've never really done anything with Access other than use it as a straight database so I'm no expert. If you write some SQL in Access, can you call that function directly in that SQL? E.g.
C#:
SELECT Import_CSV()
If so then that's exactly what you would do using ADO.NET code in C#.
 
Final working solution.
in access database created module written the below code.

C#:
option Compare Database
Sub Import_CSV(ByVal strFile As String)
'Dim strFile As String 'Filename
DoCmd.TransferText acImportDelimi, OPS_Import_Specs, _
    "tblDetail", strFile, -1
End Sub
in c# console application :
added reference :<pre>Microsoft.Office.Interop.Access</pre> right-click properties "Embed Interop Types" is changed to False.
written function in c# to call access database procedure /function.
public static int RunAccessData()
        {
 Microsoft.Office.Interop.Access.Application appAccess = null;
                appAccess = new Microsoft.Office.Interop.Access.ApplicationClass();
                string strFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"";
                Logger.WriteMessage("FilePath" + strFilePath);
                strFileName = WebConfig.GetSetting("InFileName");
                Logger.WriteMessage("FileName" + strFileName);
                strFilePathnName = strFilePath + strFileName;
                Logger.WriteMessage("FilePath and FileName" + strFilePathnName);
                appAccess.OpenCurrentDatabase("access database path here", false, null);
                object oMissing = System.Reflection.Missing.Value;
                object rt = appAccess.Run("Import_CSV", strFilePathnName);
                appAccess.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveNone);
}
so now I can able to call the access method and successfully import the data with importspec.
Thank you for all your support.
 
Back
Top Bottom