Solved

c# database (access) return results from a function

Posted on 2013-01-18
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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