使用 SQL 数据适配器构建 SQL 查询?从配置部分生成查询
我不确定我是否以正确的方式处理这个问题,但我非常开放并感谢任何建议。
我正在根据 SQL 数据库中的数据构建图表。
该图表有“年龄”和“百分位数”,或者说这是我最关心的两个字段。
我的 web.config 中有一个名为“Percentiles”的配置部分。这样可以在此配置部分中识别要显示的百分位数。例如:
<percentiles>
<add percentile="P50" visible="true">
<add percentile="P75" visible="true">
<add percentile="P85" visible="false">
<add percentile="P95" visible="true">
...
</percentiles>
我将解析百分位数列表,并希望过滤我的数据集或基于该列表生成查询。百分位数作为列存储在数据库中。例如,我希望我的查询基于该配置看起来像这样
SELECT P50, P75, P95 FROM MyData
现在我想我可以执行 SELECT * 并从我的数据集中删除这些列,或者只是不将它们作为“系列”添加到我的图表中。然而,我是一名新手开发人员,希望改进我的工作方式。根据我的经验,获取所有数据(尽管数量很少)似乎不是一个好的做法。
所以,这就是我到达 SQLDataAdapter 的地方。是否可以通过代码构建我的查询以仅包含我想要的列?我想知道是否可能的伪代码是这样的:
foreach(Percentile p in PercentileConfiguration)
{
myDataAdapter.Select.Columns.Add(p.Percentile);
}
这是一个好方法吗?非常感谢任何和所有建议!谢谢你!
I'm not sure if I'm approaching this the correct way, but am very open and appreciative of any suggestions.
I am building a chart off of data in a SQL database.
The chart has 'Age' and 'Percentile' or those are the 2 fields I am most concerned with.
I have a config section in my web.config called 'Percentiles'. This is so the percentiles to be displayed can be identified in this config section. For example:
<percentiles>
<add percentile="P50" visible="true">
<add percentile="P75" visible="true">
<add percentile="P85" visible="false">
<add percentile="P95" visible="true">
...
</percentiles>
I will parse the list of percentiles and would like to either filter my DataSet or generate my query based on that list. The percentiles are stored as columns in the database. So for example I want my query to look like this based on that configuration
SELECT P50, P75, P95 FROM MyData
Now I suppose I could just do SELECT * and remove those columns from my DataSet, or just not add them as 'Series' to my chart. However, I am a novice developer looking to improve the way I do things. In my little experience it seems grabbing all of the data (albeit a small amount) is not a good practice.
So, this is where I arrive at the SQLDataAdapter. Is it possible to build my query via code to only include the columns I want? The psuedocode I am wondering if is possible is something like this:
foreach(Percentile p in PercentileConfiguration)
{
myDataAdapter.Select.Columns.Add(p.Percentile);
}
Is this a good approach? Any and all suggestions very appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很困惑。乍一看,这似乎纯粹是一个关于如何查询数据库的问题。答案是构建一个 SQL 命令来执行此操作。我不明白为什么你要在 web.config 中存储数据库字段名称。如果您希望查询看起来像“SELECT P50, P75, P95 FROM MyData”,那么就这样做。
I'm confused. At first glance, this seems to be a question purely about how to query a database. And the answer to that is that you construct a SQL command to do that. I can't understand why you are storing database field names in the web.config. If you want a query to look like "SELECT P50, P75, P95 FROM MyData", then make it so.
SELECT * .... 会比 SELECT P50、P75 等稍微慢一些,但也只是稍微慢一些。如果您只运行一次查询(而不是在循环中多次运行或其他),并且您的表没有大量列或包含大量数据的列,那么性能损失将非常小,以至于选择特定色谱柱的努力和维护可能不值得。有人曾经告诉我,如果您必须在可读(可维护)的代码和运行速度快一毫秒的代码之间进行选择,请选择可读的代码。
也就是说,如果您要这样做,您只需构建 sql 文本,然后将其传递给您的适配器。
像这样的东西:
SELECT * .... is going to slightly slower than SELECT P50, P75, etc. But only marginally. If your only running the query once ( as opposed to multiple times in a loop, or something), and your table doesnt have a massive amount of columns, or columns with huge data, then the performance hit is going to be so nominal that the effort and maintenance in selecting specific columns will probably not be worth it. Someone once told me that if you have to choice between code that is readable(maintainable) and code that will run a millisecond faster go with the readable code.
That said, if your going to do it, you just need to construct the sql text and then pass that to your adapter.
Something like: