Advertisement
Guest User

Untitled

a guest
Mar 20th, 2015
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4.  
  5. namespace Conn_Access
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             const String connString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
  12.                                       "Data Source= COMPLETE PATH TO ACCESS FILE;" /* MAKE SURE TO CHANGE THE FILE PATH */ +
  13.                                       "Persist Security Info=False";
  14.                        
  15.             Console.WriteLine("Please enter a query: ");
  16.             String query = Console.ReadLine();
  17.  
  18.             try
  19.             {
  20.                 OleDbConnection connection = new OleDbConnection(connString);
  21.                 connection.Open();
  22.                 OleDbCommand cmd = new OleDbCommand(query, connection);
  23.                 OleDbDataAdapter a = new OleDbDataAdapter(cmd);
  24.  
  25.                 a.SelectCommand = cmd;
  26.  
  27.                 DataTable dt = new DataTable();
  28.                 a.Fill(dt); // fills the data table with the search result
  29.  
  30.                 Console.WriteLine("\nSearch result: ");
  31.  
  32.                 //foreach loop displaying the results
  33.                 foreach (DataRow dataRow in dt.Rows)
  34.                 {
  35.                     foreach (var item in dataRow.ItemArray)
  36.                     {
  37.                         Console.Write(item + " ");
  38.                     }
  39.                     Console.WriteLine();
  40.                 }
  41.  
  42.                 connection.Close();
  43.             }
  44.             catch (Exception ex)
  45.             {
  46.                 String errorMsg = ex.Message;
  47.                 Console.WriteLine(errorMsg);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement