简体   繁体   English

如何弄清楚如何在C#可移植类库中引用数据集?

[英]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. 我有3个不同的项目,它们在同一dataSet中使用相同的dataTables。

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. 目前,我的所有3个项目都有自己的数据集,我正在尝试简化它。

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 您需要创建N层架构才能解决您的问题

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 有关更多信息,请参考此MVC项目链接

You can create same in your asp.net project 您可以在asp.net项目中创建相同的项目

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! 为此+1!

But since you asked specifically about a 'portable DataSet' I translate that into 'Reusable DataSet' which imediatly points towards OOP . 但是,由于您专门询问了“便携式数据集”,因此我将其翻译为“可重用数据集”,它直接指向OOP And since C# is OO all over, you can go right ahead and create a custom DataSet : 而且由于C#遍历面向对象,因此您可以继续创建自定义DataSet

Below is a minimalistic example to encapsulate a few Access tables in a custom DataSet . 下面是一个简单示例,将一些Access表封装在一个自定义DataSet For other DBMSs change to the appropriate classes! 对于其他DBMS,请更改为适当的类!

This simple class inherits from DataSet and uses the Connectstring as the only parameter in its constructor. 这个简单的类继承自DataSet ,并将Connectstring用作其构造函数中的唯一参数。 I stores two user tables and for good measure the usertables schema. 我存储了两个用户表,并且很好地衡量了usertables模式。

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. 直接从DataSet继承是一种简单的方法。 Often it is better to create a class, that is composited from various other classes. 通常最好创建一个由各种其他类composited类。 So maybe you will end up with a Class myDbStuff with a myDataSet as one member. 因此,也许您最终将获得一个myDbStuff类, 并将 myDataSet作为一个成员。

  • Creating DB-Access objects and going for 3-Tier architecture are not mutally exclusive; 创建DB-Access对象和使用3层体系结构 并不是相互排斥的。 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. 然后在所有项目中引用生成的DLL及其名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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