Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:20:43+00:00 2026-05-27T13:20:43+00:00

I have a homework about database connection via ms access. I prepared my database

  • 0

I have a homework about database connection via ms access.

I prepared my database and saved it as dbMert and put it to debug / bin

This is my CustomerDatabase class for connecting to database:

static class CustomerDatabase
{

    static string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbMert.mdb";
    static OleDbConnection connection = null;
    static OleDbCommand command = null;


    public static void ConnectToDatabase()
    {
        if (connection == null)
        {
            connection = new OleDbConnection(connectionstring);
            command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "select * from Customer";
        }
    }     

    public static DataTable executeSelect(string sql)
    {
        ConnectToDatabase();
        DataTable dt = null;
        dt = new DataTable();
        command.CommandText = sql;
        OpenConnection();
        OleDbDataReader datareader = command.ExecuteReader();
        dt.Load(datareader);
        datareader.Close();
        CloseConnection();
        return dt;
    }

    public static void OpenConnection()
    {
        try
        {
            if (connection != null)
            {
                connection.Open();
            }
        }
        catch (Exception ex)
        {
        }
    }

    public static void CloseConnection()
    {
        if (connection != null)
        {
            connection.Close();
        }
    }


}

}

Form: In constructor i try to connect to database

public Form1()
{
    InitializeComponent();
    CustomerDatabase.ConnectToDatabase();
}

and in form’s load i try to take tuples to datagridview but nothing happens :S

private void Form1_Load(object sender, EventArgs e)
{
    string sql1 = "select * from Customer";
    DataTable dt = CustomerDatabase.executeSelect(sql1);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:20:44+00:00Added an answer on May 27, 2026 at 1:20 pm

    Try to write simple code. I suggest you to use OleDbDataAdaper, its Fill() method populate the DataTable easily.

    You may use |DataDirectory| if you are using database (.mdb) located under Bin\Debug folder.

    static string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;
                                 Data Source=|DataDirectory|\dbMert.mdb";
    

    Or

    static string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;
                                  Data Source=x:\full_path\dbMert.mdb";
    

    Or

    static string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;
                                  Data Source=x:\full_path\dbMert.mdb";
    

    static class Test
    {
        static string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbMert.mdb";
        public static DataTable executeSelect(string sql)
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter(sql,connectionString);
            adapter.Fill(dt);
            return dt;
        }
    }
    

    Add following code in form_load handler,

     string sql1 = "select * from Customer";
     DataTable dt = Test.executeSelect(sql1);
     DataGridView1.DataSource=dt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my homework and I have thought a lot about it but I
I have this homework that I gotta finish, but then something about it is
(this is indirectly a part of a much larger homework assignment) I have something
This IS NOT a Homework question! While building my current student database project I
Ok, this is for homework about hashtables, but this is the simple stuff I
It might be for pedantic purposes but not homework. I have below question about
I have in my homework some question about data structure: I have elements which
I am stumped on this homework problem. I think I have the right answer
I have this homework to school - I completed it sent it and got
I have a question about my homework problem. Here is the problem: Write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.