Solved

c# database (access) return results from a function

Posted on 2013-01-18
4
302 Views
Last Modified: 2013-01-18
Hi

I am new to c# and have (through help on this website ;o) ) created a function which connects to a database and performs an sql query.

this is fine for updates and inserts, but when I need to return data it doesn't work like I want it, I get errors because the code in the function is closing the connection, so the returned data 'expires'

here is the code:


public OleDbDataReader data_sql(string strsql)
        {
            OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\mydata.mdb");
            connection.Open();
            OleDbCommand cmd = new OleDbCommand(strsql);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = connection;
            OleDbDataReader reader = default(OleDbDataReader);
            reader = cmd.ExecuteReader();
            connection.Close();

            return reader;

        }

Open in new window


how can I get the returned data available outside of the function?

Cheers

CD
0
Comment
Question by:cycledude
  • 2
4 Comments
 
LVL 4

Expert Comment

by:DanielSV
ID: 38792129
Hi,

You need to read out the data before you close the connection.

After you do ExecuteReader();, do something like.

while(reader.Read())
{
// Pull out the data you want here.
}

See http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71).aspx for an example.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 38792135
Remove the line connection.Close(). And change ExecuteReader to be ExecuteReader(CommandBehavior.CloseConnection).
0
 

Author Comment

by:cycledude
ID: 38792149
Thanks

I have changed the function slightly to return the result, then close the connection... that appears to be working.

have also tried dale_burrell's advice, and that works too.

Thanks ;o)
0
 

Author Closing Comment

by:cycledude
ID: 38792153
many thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now