Solved

c# database (access) return results from a function

Posted on 2013-01-18
4
306 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

734 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