Created
December 16, 2012 01:17
-
-
Save cofearabi/4301879 to your computer and use it in GitHub Desktop.
(C#) connect to the access database and display values of fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Data.OleDb; | |
using System.Data; | |
using System.Xml.Serialization; | |
namespace ConsoleApplication2 | |
{ | |
public class Program | |
{ | |
private static OleDbConnection con; | |
static void Main() | |
{ | |
try | |
{ | |
con = new OleDbConnection( | |
@"Provider=Microsoft.JET.OLEDB.4.0;" | |
+ @"data source=c:\mdb\stock_mdb.mdb;Jet OLEDB" | |
+":Database Password="); | |
con.Open(); //connection must be openned | |
OleDbCommand cmd = new OleDbCommand( | |
"SELECT * from meigara", con); | |
OleDbDataReader reader = cmd.ExecuteReader(); | |
while(reader.Read()) // if can read row from database | |
{ | |
Console.WriteLine(reader.GetValue(1).ToString() | |
+ " = " + reader.GetValue(2).ToString()); | |
} | |
} | |
catch(Exception ex) | |
{ | |
Console.WriteLine("Ex: "+ex); | |
} | |
finally | |
{ | |
con.Close(); // finally closes connection | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment