简体   繁体   中英

How to figure out how to reference a dataSet in a C# portable class library?

I have 3 different projects that use the same dataTables in the same dataSet.

I'm trying to create a portable library to cut down on the same code across the different projects.

I'm having trouble figuring out how to include a reference for the data-set?

Currently all 3 of my projects have their own data-set and I'm trying to simplify it.

I hope that I'm making sense.!

Please guide me with best possible way..

You need to create N-Tier architecture will solve your problem

Your project architecture should be like this ,

Solution(your project) 
|
|-  Project 1
|
|-  Project 2
|
|-  Project 3
|
|-  DAL - //Data access layer 
|
|-  BL - //your business logic will be here 

and just need reference where you want to use eg

yourproject.Dal.yourclass.yourmethod()

for more information just refer this MVC project link

You can create same in your asp.net project

First let me note, that Imran's advice is solid and you should definitely consider going this way; it is clear and offers a good path for growth. +1 for this!

But since you asked specifically about a 'portable DataSet' I translate that into 'Reusable DataSet' which imediatly points towards OOP . And since C# is OO all over, you can go right ahead and create a custom DataSet :

Below is a minimalistic example to encapsulate a few Access tables in a custom DataSet . For other DBMSs change to the appropriate classes!

This simple class inherits from DataSet and uses the Connectstring as the only parameter in its constructor. I stores two user tables and for good measure the usertables schema.

Obviously you will want to expand this greatly..

public class myDataSet : DataSet
{

    OleDbConnection conn;
    OleDbDataAdapter DBDA;
    OleDbCommand SqlCmd;
    string ConnectionString = @"your default connection string here!";

    public myDataSet (string connectString)
    {
        conn = new OleDbConnection();

        if (String.IsNullOrEmpty(connectString)) 
                conn.ConnectionString = ConnectionString;
        else conn.ConnectionString = connectString;

        connectMe();

        DataTable userTables = conn.GetSchema("Tables");

        SqlCmd = new OleDbCommand("select * from [Names]", conn);
        DBDA = new OleDbDataAdapter(SqlCmd);
        DataTable Names = new DataTable("Names");
        DBDA.Fill(Names); 

        SqlCmd = new OleDbCommand("select * from [Places]", conn);
        DBDA = new OleDbDataAdapter(SqlCmd);
        DataTable Places= new DataTable("Places");
        DBDA.Fill(Places);

        conn.Close();

        this.Tables.Add(userTables);
        this.Tables.Add(Names);
        this.Tables.Add(Places);

    }

    public bool connectMe()
    {
        try { conn.Open();  } 
        catch { /* your error hanfdilng here! */}
        if (conn.State == ConnectionState.Open) return true;
        return false;
    }

}

Since it is a regular class you can add Properties and Methods as you like; for re-retrieveing as well as Updating etc.. As usual class design is an art an will take some consideration as well as repeated refinements. Your current needs are a good starting point but planning for growth will pay.

Two more notes:

  • Inheriting directly from DataSet is a simple way. Often it is better to create a class, that is composited from various other classes. So maybe you will end up with a Class myDbStuff with a myDataSet as one member.

  • Creating DB-Access objects and going for 3-Tier architecture are not mutally exclusive; in fact they go very well together; just make sure you know in time where the sparating lines will go..

To make the example work do this:

  • Create a class library project with the neccessary refences and uses clauses and compile it.
  • Then refer to the resulting DLL and the namespace of it in all your projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM