Design question --- Return a DataSet?

  • Thread starter Thread starter JS
  • Start date Start date
J

JS

I'm trying to create a data layer and having problems returning a DataSet
from my code that's in a class module.

Please forgive me. I'm new to C# (VB'er). I decided to create my data layer
in small steps. Right now, I'm just trying to attach a ComboBox to a dataset
that's in my class module. In the class, I call a Stored Procedure. I know
how to set up the connection, command, adapter, and dataset, what I'm having
a problem with is, telling the method in my class to return the dataset to
the caller.

In VB I could return the dataset by creating a function like such:
This code is in a class module.

Function ShowVendors() as DataSet
Connection
Command code
Adapter code

Return myDataSet
End Function

I then set the ComboBox datasource to ShowVendors(). Works like a charm.
But, I cannot for the life of me get ShowVendors() to return the dataset
back to the caller in C# I have seen and tried a million examples (give or
take a hundred or so) examples on how to bind a control to a dataset, but I
don't know how many more "Console.WriteLine" or code behind the form
examples I can take. I want to keep all of my database connections totally
separate from my forms.

So, I guess my question is two part. How do I return a dataset from my data
layer, and is returning a dataset like I'm trying to here a good or bad
design approach?

Any help on this would be greatly appreciated. TIA,

- JS -
 
Hi,

As for design issues, I think it would be better to return a DataTable - as
you don't need data relations and multiple tables. But as for coding, there
is no big difference in returning either a DataSet or a DataTable (both ways
are simple like 1,2,3):

using System;
// You might need to add a reference to System.Data.dll to your
// project.
using System.Data;

namespace MyDataLayer
{
public class MyDataObject
{
// Constructors, class members and other stuff...

public DataSet GetCustomers()
{
DataSet customers = new DataSet();

// Create connection, command, data adapter, whatever to populate the
dataset.

return customers;
}
}
}

In the example above, you can easily replace DataSet with DataTable. Hope
this helps.
 
JS... I've wrapped up a few data classes that return objects as you're
trying to do as well as completely separate the data acquisition from
the presentation. Your form only needs to know what a datatable or
datareader is... nothing beyond that.

You're welcome to download and use them as they are or use them as a
starting point or reference for creating your own. They're not
all-inclusive, but they work for many of the projects I've worked on
and used them in.

http://www.chornbe.com/softdownloads.aspx then click on DBTools

I hope that helps.
 
Back
Top