简体   繁体   English

动态查询SQL服务器

[英]Query SQL Server Dynamically

I'm working on creating a dashboard.我正在创建仪表板。 I have started to refactor the application so methods relating to querying the database are generic or dynamic.我已经开始重构应用程序,因此与查询数据库相关的方法是通用的或动态的。

I'm fairly new to the concept of generics and still an amateur programmer but I have done some searching and I have tried to come up with a solution.我对 generics 的概念还很陌生,仍然是一名业余程序员,但我已经进行了一些搜索,并试图提出解决方案。 The problem is not really building the query string in a dynamic way.问题并不是真正以动态方式构建查询字符串。 I'm fine with concatenating string literals and variables, I don't really need anything more complex.我可以连接字符串文字和变量,我真的不需要更复杂的东西。 The bigger issue for me is when I create this query, getting back the data and assigning it to the correct variables in a dynamic way.对我来说更大的问题是当我创建这个查询时,获取数据并以动态方式将其分配给正确的变量。

Lets say I have a table of defects, another for test cases and another for test runs.假设我有一个缺陷表,另一个用于测试用例,另一个用于测试运行。 I want to create a method that looks something like:我想创建一个看起来像这样的方法:

public void QueryDatabase<T>(ref List<T> Entitylist, List<string> Columns, string query) where T: Defect, new()

Now this is not perfect but you get the idea.现在这并不完美,但你明白了。 Not everything about defects, test cases and test runs are the same but, I'm looking for a way to dynamically assign the retrieved columns to its "correct" variable.并非关于缺陷、测试用例和测试运行的所有内容都是相同的,但是,我正在寻找一种将检索到的列动态分配给其“正确”变量的方法。

If more information is needed I can provide it.如果需要更多信息,我可以提供。

You're re-inventing the wheel.你在重新发明轮子。 Use an ORM, like Entity Framework or NHibernate.使用 ORM,如 Entity Framework 或 NHibernate。 You will find it's much more flexible, and tools like that will continue to evolve over time and add new features and improve performance while you can focus on more important things.您会发现它更加灵活,并且这样的工具将随着时间的推移不断发展,并添加新功能并提高性能,同时您可以专注于更重要的事情。

EDIT :编辑
Although I think overall it's important to learn to use tools for something like this (I'm personally a fan of Entity Framework and have used it successfully on several projects now, and used LINQ to SQL before that), it can still be valuable as a learning exercise to understand how to do this.虽然我认为总的来说学习使用这样的工具很重要(我个人是 Entity Framework 的粉丝,现在已经在几个项目中成功使用它,并且在此之前使用过 LINQ 到 SQL),它仍然很有价值,因为一个学习练习,以了解如何做到这一点。 The ORMs I have experience with use XML to define the data model, and use code generation on the XML file to create the class model. The ORMs I have experience with use XML to define the data model, and use code generation on the XML file to create the class model. LINQ to SQL uses custom attributes on the code-generated classes to define the source table and columns for each class and property, and reflection at run-time to map the data returned from a SqlDataReader to the properties on your class object. LINQ to SQL uses custom attributes on the code-generated classes to define the source table and columns for each class and property, and reflection at run-time to map the data returned from a SqlDataReader to the properties on your class object. Entity Framework can behave differently depending on the version you're using, whether you use the "default" or "POCO" templates, but ultimately does basically the same thing (using reflection to map the database results to properties on your class), it just may or not use custom attributes to determine the mapping.实体框架的行为取决于您使用的版本,无论您使用“默认”模板还是“POCO”模板,但最终做的事情基本相同(使用反射到 map 数据库结果到你的类的属性),它只是可能使用或不使用自定义属性来确定映射。 I assume NHibernate does it the same way as well.我假设 NHibernate 也是这样做的。

You are reinventing the wheel, yes, it's true.你在重新发明轮子,是的,这是真的。 You are best advised to use an object-relational mapper off the "shelf".最好建议您使用现成的对象关系映射器。 But I think you also deserve an answer to your question: to assign the query results dynamically to the correct properties, you would use reflection.但我认为您也应该回答您的问题:要将查询结果动态分配给正确的属性,您将使用反射。 See the documentation for the System.Reflection namespace if you want more information.如果您想了解更多信息,请参阅 System.Reflection 命名空间的文档。

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

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