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 8311009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:40:01+00:00 2026-06-08T19:40:01+00:00

For a reason I can’t use my MySQL-DB, so I switched to a Access

  • 0

For a reason I can’t use my MySQL-DB, so I switched to a Access DB.

I wanted to have some small functions that just do simple Queries like SELECT, UPDATE and INSERT.

I followed this small code:

using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

class Csharp_Access
{
    public void Csharp_Access_Datenbank()
    {
        OleDbConnection con = new OleDbConnection(
        @"Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=C:\data.mdb");
        con.Open();

        string strSQL = "SELECT * FROM Tabelle1";

        OleDbCommand cmd = new OleDbCommand(strSQL, con);
        OleDbDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            MessageBox.Show(dr[0].ToString());
        }
        dr.Close();
        con.Close();
        }
    }

When I just Copy&Paste it, this code works perfectly.
So I decided to but that code into a “dbFunctions.cs”:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Xml.Serialization;

namespace AquadoServerTool
{
    class dbFunctions
    {
        /// <summary>
        /// Funktion, um eine Abfrage auszuführen
        /// </summary>
        /// <param name="QueryStr">SQL-String</param>
        /// <returns></returns>
        public static OleDbDataReader QueryString(string QueryStr)
        {
            // string strAccessSelect = "SELECT * FROM seriennummer";

            // Verbindung zur Datenbank aufbauen
            OleDbConnection con = null;
            try
            {
                con = new OleDbConnection(GlobalVar.strAccessConn);
                con.Open();
            }
            catch (Exception)
            {
                return null;
            }

            OleDbCommand cmd = new OleDbCommand(QueryStr, con);
            OleDbDataReader dr = cmd.ExecuteReader();
            // while (dr.Read())
            // {
            //     MessageBox.Show(dr[0].ToString());
            // }

            con.Close();

            return dr;
        }
    }

}

For testing, I made a little button if that works:

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbDataReader dr = dbFunctions.QueryString("SELECT * FROM seriennummer;");

        while (dr.Read())
        {
            MessageBox.Show(dr[1].ToString());
        }
        dr.Close();
    }

Yeah, now click that button to oblivion!
But I get the error, that Read() won’t work, because it has been already closed.

Is my code simply wrong or did I just forgot something?

Greetings,
Trollwut

  • 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-06-08T19:40:03+00:00Added an answer on June 8, 2026 at 7:40 pm

    Your problem is that you first close the connection, and then try to read from the DataReader. You have to keep the connection open to read.


    You could create a wrapper around the DataReader that implements IDisposable and closes the connection for you.

    Simple example:

    class Wrapper : IDisposable
    {
        public OleDbDataReader Reader { get { return reader; } }
        private OleDbConnection connection;
    
        public Wrapper(OleDbConnection connection, string QueryStr)
        { 
            this.connection = connection; 
            OleDbCommand cmd = new OleDbCommand(QueryStr, connection);
            OleDbDataReader dr = cmd.ExecuteReader();
        }
    
        public void Dispose()
        {
            reader.Dispose();
            connection.Dispose();
        }
    }
    
    class dbFunctions
    {
        public static OleDbDataReader QueryString(string QueryStr)
        {
            OleDbConnection con = new OleDbConnection(GlobalVar.strAccessConn);
            con.Open();
            return new Wrapper(con, QueryStr);
        }
    }
    

    Usage:

    private void button1_Click(object sender, EventArgs e)
    {
        using (var dr = dbFunctions.QueryString("SELECT * FROM seriennummer;"))
            while (dr.Reader.Read())
                MessageBox.Show(dr[1].ToString());
    }
    

    Another way is to simple return the data via a DataTable.

    class dbFunctions
    {
        public static DataTable QueryString(string QueryStr)
        {
            var result = new DataTable();
            using(OleDbConnection con = new OleDbConnection(GlobalVar.strAccessConn))
            {
                con.Open();
                using(OleDbCommand cmd = new OleDbCommand(QueryStr, con))
                    result.Load(cmd.ExecuteReader());
            }
            return result;
        }
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        var data = dbFunctions.QueryString("SELECT * FROM seriennummer;");
        foreach(var row in data.Rows)
            MessageBox.Show(row[1].ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason i can't seem to get this right ok i have 2
For some reason I can't get static_dir to work. In my app.ymal I have:
So I have this bit of code, that for some reason won't compile, because
For some reason I can't get this to work at all. I have read
Hi got a weird problem parsing JSON for some reason can't access the values
This question is pretty simple I for some reason can't get the proper result
For some odd reason I can no longer access my images in my image
I'm dealing with a scrupt that for some reason can't read normal $_GET and
There are some breakpoints in my project that for some reason can not be
For some reason I can't use runat=server as an attribute for the input tag

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.