DataAdapter 从基表架构中选择字符串?

发布于 2024-07-16 02:30:53 字数 730 浏览 12 评论 0原文

当我构建 .xsd 时,我必须为每个表选择列,并且它为表创建了架构,对吗? 那么,如何才能将该 Select 字符串用作数据适配器新实例的基本 Select 命令,然后根据需要向其附加Where 和 OrderBy 子句呢?

这将使我不必使每个 DataAdapter 的字段列表(针对同一个表)与 .xsd 文件中该表的架构保持同步。

有多个 DataAdapter 在某个表模式上工作,但在Where和OrderBy子句中具有不同的参数,这不是很常见吗? 当然,人们不必为所有都使用同一表模式的六个 DataAdapter 维护(甚至冗余构建)选择字符串的字段列表部分。

我正在设想类似这样的伪代码:

BaseSelectString = MyTypedDataSet.JobsTable.GetSelectStringFromSchema()  // Is there such a method or technique?

WhereClause = " Where SomeField = @Param1 and SomeOtherField = @Param2"
OrderByClause = " Order By Field1, Field2"

SelectString=BaseSelectString + WhereClause + OrderByClause

OleDbDataAdapter adapter = new OleDbDataAdapter(SelectString, MyConn)

When I built my .xsd, I had to choose the columns for each table, and it made a schema for the tables, right? So how can I get that Select string to use as a base Select command for new instances of dataadapters, and then just append a Where and OrderBy clause to it as needed?

That would keep me from having to keep each DataAdapter's field list (for the same table) in synch with the schema of that table in the .xsd file.

Isn't it common to have several DataAdapters that work on a certain table schema, but with different params in the Where and OrderBy clauses? Surely one does not have to maintain (or even redundently build) the field list part of the Select strings for half a dozen DataAdapters that all work off of the same table schema.

I'm envisioning something like this pseudo code:

BaseSelectString = MyTypedDataSet.JobsTable.GetSelectStringFromSchema()  // Is there such a method or technique?

WhereClause = " Where SomeField = @Param1 and SomeOtherField = @Param2"
OrderByClause = " Order By Field1, Field2"

SelectString=BaseSelectString + WhereClause + OrderByClause

OleDbDataAdapter adapter = new OleDbDataAdapter(SelectString, MyConn)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2024-07-23 02:30:53

每个表都有一个默认查询(顶部带有检查的查询)。 当您将表拖入数据集以创建查询时,它会编写一条 SQL 语句,用于架构您的表。 保持该查询简单,您可能实际上不会在代码中使用它,并且您始终可以编辑该查询以更新表架构。

每次打开默认查询时,它都会连接到您的数据源,并允许您选择以前不存在的新列。 如果要更新现有列,请在尝试打开查询之前从表中删除所有列。 当您保存查询时,更新的列将被添加回来。

确保您的连接字符串有权查看列信息。

您可以将多个查询添加到单个 TableAdapter。 设计器中的 TableAdapter 出现分段,表架构位于顶部,查询位于底部。 默认查询将控制哪些列可用于其他查询的输出。 要添加其他查询,请右键单击 TableAdapter 并选择“添加->查询”,或者如果您选择 TableAdapter 的底部,则可以选择“添加查询...”。 您创建的任何新 SQL 查询都将从默认查询中的 SQL 开始。 您为每个新查询指定一个方法名称,您可以使用该名称来代替默认查询的“Fill”或“GetData”方法。 假设每个新查询都将具有与默认查询匹配的结果集,即使它们可以具有不同的“where”子句参数。

简而言之,

  • 每个表可能有一个 TableAdapter,只需添加多个查询即可。
  • 每个附加查询可以有不同的“Where”子句参数,只要它们都返回相同的列即可。

Each table has a default query (The one on top with the check on it). When you dragged your tables in to the dataset to create the query, it wrote a SQL statement which it uses to schema your table. Keep that query simple, you might not actually use it in code, and you can always edit that query to update the table schema.

Every time you open the default query it connects to your datasource and allows you to select new columns that weren't in there before. If you want to update your existing columns, delete all the columns out of the table before you attempt to open the query. When you save the query, your updated columns get added back.

Make sure your connection string has permissions to view column information.

You can add multiple queries to a single TableAdapter. TableAdapters in the designer appear sectioned with a table schema at the top, and queries on the bottom. The default query will control which columns are available for output from the other queries. To add an additional query, right click on the TableAdapter and select "Add->Query" or if you are selecting the bottom part of the TableAdapter you can select "Add Query...". Any new SQL query you create will start off with the SQL from the default query. You give each new query a method name which you can use instead of the default query's "Fill" or "GetData" methods. The assumption is that each new query will have a result set that matches the default query even though they can have different "where" clause parameters.

In short

  • You may have a single TableAdapter for each table, just add multiple queries.
  • Each additional query can have different "Where" clause parameters as long as they all return the same columns.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文