我有一个 ASP.NET 3.5 应用程序,我希望允许用户选择一个表并允许对该表进行 CRUD 操作。 用户将被限制只能编辑多个表,但是只有在部署之后才能知道这些表甚至数据库; web.config 将设置连接和表。
因此,我需要构建一个框架,允许更新 SQL 数据库中的通用表。 我见过的大多数 ORM 都要求在编译时了解架构,因此我无法使用它们。 最好的答案似乎是动态构建 SQL 查询字符串并使用 reg 表达式来确保有效输入并防止 SQL 注入。 有没有更好的办法?
I have an ASP.NET 3.5 application that I want to allow the user to select a table and allow CRUD operations on that table. The user will be restricted to a number of tables to edit however the tables and even database won't be known until after deployment; the web.config will setup the connection and tables.
So I need to build a framework that will allow a generic table in a SQL database to be updated. Most of the ORMs I've seen require the schema to be known at compile time so I cannot use these. The best answer seems to be build the SQL query strings dynamically and use reg expressions to ensure valid input and protect against SQL injections. Is there a better way?
发布评论
评论(3)
是的。 数据集和数据表对象怎么样? 您可以通过动态 sql 语句填充它们,将它们直接绑定到良好的编辑器网格控件,并且架构和列数据类型检查应该很简单。 在 ORM 和 Linq 之前我们做过什么?!?!
Yes. How about dataset and datatable objects? you can populate these via a dynamic sql statement, bind them directly to a good editor grid control, and the schema and column data type checking should be a snap. What did we ever do before ORM's and Linq?!?!
动态 Linq 库令人难以置信。 您可以将所有内容指定为字符串,除了表名称。
(来源:scottgu.com)
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
但是,请在此处查找有关如何通过动态映射 DataContext 在运行时选择表的想法:
http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/09/27/25294.aspx
The Dynamic Linq Library is incredible. You can specify everything as a string, except maybe the table names.
(source: scottgu.com)
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
However, look here for ideas on how to select tables at runtime by dynamically mapping the DataContext:
http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/09/27/25294.aspx
您可以使用 JJMasterData 为表中的所有 CRUD 操作动态生成 SQL 脚本。 我们支持 .NET 6 和 .NET Framework 4.8。
https://www.github.com/jjconsulting/JJMasterData
You can use JJMasterData to dynamic generate SQL scripts for all CRUD operations from a table. We support both .NET 6 and .NET Framework 4.8.
https://www.github.com/jjconsulting/JJMasterData