Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.OleDb;
- namespace Conn_Access
- {
- class Program
- {
- static void Main()
- {
- const String connString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
- "Data Source= COMPLETE PATH TO ACCESS FILE;" /* MAKE SURE TO CHANGE THE FILE PATH */ +
- "Persist Security Info=False";
- Console.WriteLine("Please enter a query: ");
- String query = Console.ReadLine();
- try
- {
- OleDbConnection connection = new OleDbConnection(connString);
- connection.Open();
- OleDbCommand cmd = new OleDbCommand(query, connection);
- OleDbDataAdapter a = new OleDbDataAdapter(cmd);
- a.SelectCommand = cmd;
- DataTable dt = new DataTable();
- a.Fill(dt); // fills the data table with the search result
- Console.WriteLine("\nSearch result: ");
- //foreach loop displaying the results
- foreach (DataRow dataRow in dt.Rows)
- {
- foreach (var item in dataRow.ItemArray)
- {
- Console.Write(item + " ");
- }
- Console.WriteLine();
- }
- connection.Close();
- }
- catch (Exception ex)
- {
- String errorMsg = ex.Message;
- Console.WriteLine(errorMsg);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement