Search code examples
c#sqldatabasedatabase-programming

Trying to connect to a Access database table in order to retrieve data, but encounter difficulties


First time doing database programming, so I just made a database in Access to try to do something with it. The database I created called "TestDatabase" on my desktop, and the table I created inside this database called "TestTable". And here is my code:

using System;
using System.Data;
using System.Data.SqlClient;

namespace DatabaseTest
{
    class Test
    {
        static void Main(string[] args)
        {
            // I don't know if my connection is correct or not. My access database is on my local desktop though
            string connectionString = "Data Source = (local); Initial Catalog = TestDatabase; Integrated Security = SSPI";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlDataReader reader = null;
                SqlCommand command = new SqlCommand("SELECT * from TestTable", connection);

                connection.Open();
                try
                {
                    reader = command.ExecuteReader();
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.ToString());
                }

                // print all the data in the table
                while (reader.Read())
                {
                    Console.Write(reader[0].ToString() + ", ");
                    Console.Write(reader[1].ToString() + ", ");
                    Console.Write(reader[2].ToString() + ", ");
                    Console.Write(reader[3].ToString() + ", ");
                    Console.WriteLine(reader[4].ToString());
                }
            }
            Console.ReadLine();
        }
    }
}

And here is my table looks like, if you wonder: (just a toy example)

ID   First Name  Last Name   Age     Friend
1    Leon        Ma          18      Yes
2    Amy         Jane        16      No
3    David       Zhang       20      No
4    Alan        Yue         19      Yes

However, it doesn't work, because nothing appear on my console. What did I do wrong. Really need some help. Thanks.


Solution

  • you need something like the following to connect to Access DB

    conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DbPath\SomeAccessFileName.accdb")
    

    Config file would be setup like this for Standard security

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
    Persist Security Info=False;
    

    Reference Link Access connection strings

    Usage for creating the connection objects in Code Behind

    using System.Data.Odbc;
    
    using(OleDbConnection connection = new OleDbConnection(con))
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand("SELECT * from TestTable", connection) 
        using(OleDbDataReader reader = command.ExecuteReader())
        {
             while(reader.Read())
             {
                Console.Write(reader[0].ToString() + ", ");
                Console.Write(reader[1].ToString() + ", ");
                Console.Write(reader[2].ToString() + ", ");
                Console.Write(reader[3].ToString() + ", ");
                Console.WriteLine(reader[4].ToString());
             }
        }
    }