Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

DataAccess Classes and inheritance?

$
0
0

Hi,

I'm trying to build a simple program that pulls some data from our database and displays it in a grid.  I recently read a blog that suggested building DataAccess Classes to help separate that functionallity from the presentation layer... made sense to me so I'm trying to model it myself.

Here is what I have:

A data access class that uses App.config to get an SqlConnection.

using System.Data.SqlClient;

using System.Configuration;

namespace MyDAL

{

     public class MyDBAccess

     {

         protected SqlConnection oConnection;

         private string cnString = string.Empty;

         public MyDBAccess()

         {

             cnString = ConfigurationManager.ConnectionStrings[“MyDBProvider”].ConnectionString;

             this.oConnection = new SqlConnection(cnString);

         }

     }

}

I sub-class this to another class that adds the SQL commands and fills a DataSet and returns it to the calling program.

using System.Data.SqlClient;

using System.Data;

namespace MyUsersDAL

{

    Public class MyUsersDAL : MyDBAccess

    {

        Private DataSet ds = null;

       

        Public GetUsers()

        {

            String sql = “Select * from users”;

            SqldataAdapter adapter = new SqlDataAdapter(sql, this.oConnection);

            Ds = new DataSet();

            Adapter.fill(ds, “UsersTable”);

            Return ds

         }

    }

}

I added a reference to MyDAL to this class.

Then lastly I created a Windows Form project, added a grid to the form and added a reference to MyUsersDAL with the following code in the MainForm() initializer.

MyUsersDAL dal = new MyUsersDAL();

dataGridView1.DataSource = dal.GetUsers().Tables["UsersTable"];

When I try to run the form I get an error message that states, "MyDAL.MyDBAccess is defined in an assembly that is not referenced.  You must add a reference to assembly 'MyDAL', Version 1.0.0.0, Culture=neutral, PublicKeyToken=null'."

So, what am I missing here?  Do I really need to add a reference in my form project for every base class that I subclass?  Doesn't the reference to MyDAL in the subclass MyUsersDAL satisfy that requirement?  Am I making any other glaring mistakes?

Thanks in advance,

Linn


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>