Skip to content

Instantly share code, notes, and snippets.

@cofearabi
Created December 16, 2012 01:17
Show Gist options
  • Save cofearabi/4301879 to your computer and use it in GitHub Desktop.
Save cofearabi/4301879 to your computer and use it in GitHub Desktop.
(C#) connect to the access database and display values of fields.
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