.Net:如何创建独立于供应商的数据集、表格适配器、绑定(数据库在运行时决定)适配器、绑定、表格、独立

2023-09-08 08:31:13 作者:爱已走到尽头

我有一个 C# Windows Forms 应用程序,它的原型是在 SQL Server(强类型数据集)上创建的.在其最终版本中,应用程序必须能够在 SQL Server、MySQL 或 Oracle 上运行.

I have a C# Windows Forms application, whose prototype was created on SQL Server (strongly-typed dataset). In its final version, the application must be able to work over SQL Server, MySQL or Oracle.

现在我想知道原型中的哪些部分(如果有的话)可以重复使用.1. 数据集(类型化) ?2. 表适配器?(可能不是,它们包含特定于 SQL Server 的语法)3. 绑定到 DataGridViews

Now I am wondering which parts (if any) can be reused from the prototype. 1. Dataset (typed) ? 2. TableAdapters? (probably not, they contain SQL Server-specific syntax) 3. Bindings to DataGridViews

最重要的是,如果我们需要重新实现这一切,有没有办法在设计时做到这一点?或者,1. 我们需要以编程方式创建无类型数据集吗?2. 我们是否需要以编程方式创建它的数据适配器(或表适配器)?如果是,两者中的哪一个?3. 我们是否需要以编程方式创建其与界面的 datagridviews 的绑定?

Most importantly, if we need to re-implement all this, is there a way to do this at design-time? Or, 1. do we need to programmatically create untyped-dataset? 2. do we need to programmatically create its data adapters (or table adapters)? If yes, which of the two? 3. do we need to programmatically create its bindings to the datagridviews of the interface?

也许无关紧要:如果我们从现有的数据库模式创建一个实体模型(AFAIK,它提供数据库独立性),我们可以使用它以某种方式创建与我们的数据网格视图的绑定吗?

Perhaps irrelevant: if we create a entity model (AFAIK it provides db independence) from the existing db schema, could we use this somehow to create bindings to our datagridviews?

谢谢!

那么,为了保留我们的Bindings和dataGridViews,以及我们已经实现的一些额外的逻辑,我们是不是应该把所有生成的TableAdapter都扔掉,手动编写呢?如果我们真的扔掉它们,我们应该使用 DataAdapters 来代替吗?

So, in order to keep our Bindings and dataGridViews, as well as some additional logic we have implemented, should we throw away all the generated TableAdapters and write them manually? If we do throw them away, should we use DataAdapters instead?

这是一种按部就班"的方法吗?有人做过这样的事吗?

Is this a "by-the-book" approach? Has anyone done something like this?

更一般地说,如果您需要创建一个表单应用程序以在多个数据库中工作,您会这样做吗: A. 使用非类型化数据集、数据适配器/表适配器和手动创建的绑定 B. 以某种方式生成独立于供应商的数据集和数据适配器/tableadapters (如何?)并在设计时通过 VS gui C 绑定它们.其他方式???

More generally, if you need to create a Forms application to work in multiple dbs, would you do it: A. with untyped dataset, dataadapters/tableadapters and bindings created by hand B. somehow generate a vendor-independent dataset and dataadapters/tableadapters (how?) and bind them at design time through the VS gui C. some other way???

更新:

那么,为了保留我们的Bindings和dataGridViews,以及我们已经实现的一些额外的逻辑,我们是不是应该把所有生成的TableAdapter都扔掉,手动编写呢?如果我们真的扔掉它们,我们应该使用 DataAdapters 来代替吗?

So, in order to keep our Bindings and dataGridViews, as well as some additional logic we have implemented, should we throw away all the generated TableAdapters and write them manually? If we do throw them away, should we use DataAdapters instead?

这是一种按部就班"的方法吗?有人做过这样的事吗?

Is this a "by-the-book" approach? Has anyone done something like this?

更一般地说,如果您需要创建一个表单应用程序以在多个数据库中工作,您会这样做吗: A. 使用非类型化数据集、数据适配器/表适配器和手动创建的绑定 B. 以某种方式生成独立于供应商的数据集和数据适配器/tableadapters (如何?)并在设计时通过 VS gui C 绑定它们.其他方式???

More generally, if you need to create a Forms application to work in multiple dbs, would you do it: A. with untyped dataset, dataadapters/tableadapters and bindings created by hand B. somehow generate a vendor-independent dataset and dataadapters/tableadapters (how?) and bind them at design time through the VS gui C. some other way???

推荐答案

类型化的数据集/表独立于数据库.(但是,如果您在设计器中添加适配器,它们将获得特定于数据库的内容.不要使用设计器中的适配器适配器不独立于数据库.数据绑定独立于数据库.但要注意拖放数据绑定会自动添加适配器

我的建议:

从数据集中删除适配器设计师使用具有获取/填充方法的简单类重写您自己的存储库/适配器表.所以你使用它们而不是生成的适配器.这些类可以是特定于 DB 的.因此,例如 PersonRepositorySqlServer、PersonRepositoryMySql.或者也许你给 db-type 和构造函数以尽可能重用 SQL..如果您在表单上使用了适配器,删除它们.手工编码填充物数据集

我总是回答其他问题

What I always do to answer the rest of the questions

我使用类型化的数据集,但我只是制作表格而不是适配器我通常对数据绑定进行编码,因为有时设计者会搞砸,但这不是独立于数据库的必要条件我编写了自己的存储库,使用适配器来填充/获取/更新数据表.然而我手工编码.给定一个类型化的数据表,顺便说一下,自动生成更新/插入/删除/填充语句相当容易..

重写适配器看起来很难,但实际上非常可行.

Rewriting the adapters looks hard but is actually pretty doable.