This article is a mirror article of machine translation, please click here to jump to the original article.

View: 1|Reply: 0

[Source] .NET/C# uses SqlScriptDOM to parse executed SQL statements

[Copy link]
Posted on46 seconds ago | | | |
Requirements: You need to determine whether there are any syntax errors in the SQL statement to ensure that it can be executed normally in the SQL Server database, and then obtain the table name of the execution statement.

SqlScriptDOM

SqlScriptDOM (SQL Script Document Object Model) is a part of the .NET framework that is mainly used to parse and manipulate T-SQL scripts. It provides a set of APIs that allow developers to programmatically read, modify, or generate T-SQL code for SQL Server. This is useful in situations where database scripting needs to be automated, such as in database migration tools, database versioning, or any application where SQL statements need to be dynamically constructed.

With SqlScriptDOM, you can:

Parse SQL scripts: Convert T-SQL code into a tree structure for easy understanding and processing.
Traversing AST (Abstract Syntax Tree): Access and modify different parts of your SQL script by traversing this tree structure.
Generate SQL scripts: Regenerate T-SQL code based on the modified abstract syntax tree.
Perform static analysis: Check for potential errors or irregularities in SQL scripts without actually running them.
Support for a variety of SQL elements, including but not limited to table definitions, views, stored procedures, etc.

Source:The hyperlink login is visible.
Documentation:The hyperlink login is visible.

Version mapping reference

SqlVersionFlags Quick Reference:

LogoSQL Server versionyearCommon characteristics
TSql80 and above20002000Basic T-SQL
TSql90 and above20052005XML、CTE
TSql100 and above20082008Merge, file flow
TSql110 and above20122012sequences, window functions
TSql120 and above20142014Memory OLTP, maximum duration
TSql130 and above20162016JSON, tense table
TSql140 and above20172017Figure, STRING_AGG
TSql150 and above20192019UTF-8, Smart QP
TSql160 and above20222022Recoverable constraints, JSON improvements
TSql170 and above20252025Vector search, AI features


NUGET references as follows:
Parse table names

Parse the table name in the select statement with the following code:
As shown below:



Verify SQL query security, only allow execution of SELECT class (untested), the code is as follows:
(End)




Previous:Launch a Mozilla Firefox browser using Docker
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com