C# To Open Access Database
visual studio 2015 , access 2013. i in need of opening database in order read data tables. my code below, keeps giving me error , never open database. what need in order able open access database in order read data tables?
error: (this error thrown when code hits line dd= db.open...)
an unhandled exception of type 'system.runtime.interopservices.comexception' occurred in linktosql.exe
additional information: unrecognized database format 'r:\test.accdb'.
syntax:
using system; using system.collections.generic; using system.data; using system.data.oledb; using system.io; using system.text; namespace linktosql { class program { static void main(string[] args) { openaccess(); } public static void openaccess() { string path = "r:\\test.accdb"; dao.database dd; dao.dbengine db = new dao.dbengine(); dd = db.opendatabase(path); } } }
hello,
as mentioned already, use oledb manage data provider. in following example work done in class data operations. oledbconnectionstringbuilder creates our connection, in case in same folder executable can point wherever want. last line (looks not needed here) hide column if used in datagridview.
public class dataoperations { private oledbconnectionstringbuilder builder = new oledbconnectionstringbuilder(); public dataoperations() { builder.provider = "microsoft.ace.oledb.12.0"; builder.datasource = path.combine(appdomain.currentdomain.basedirectory, "database1.accdb"); } public datatable datatable { get; set; } public void getdata() { datatable = new datatable(); using (oledbconnection cn = new oledbconnection { connectionstring = builder.connectionstring }) { var sql = "select identifier, firstname, lastname, lastorderdate customers;"; using (oledbcommand cmd = new oledbcommand { commandtext = sql, connection = cn }) { cn.open(); datatable.load(cmd.executereader()); datatable.columns["identifier"].columnmapping = mappingtype.hidden; } } } }
please remember mark replies answers if , unmark them if provide no help, others looking solutions same or similar problem. contact via twitter (karen payne) or facebook (karen payne) via msdn profile not answer coding question on either.
vb forums - moderator
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment