简体   繁体   English

验证Microsoft Access SQL

[英]Validate Microsoft Access SQL

I have a long set of SQL scripts. 我有很长的SQL脚本集。 They are all update statements. 它们都是更新语句。 It's for an access database. 用于访问数据库。 I want to validate the script before I run it. 我想在运行脚本之前先对其进行验证。 Firstly, I'd like to make sure that the query can be parsed. 首先,我想确保查询可以被解析。 Ie that the SQL is at least syntactically correct. 也就是说,SQL至少在语法上是正确的。 Secondly, I'd like to make sure that the query is valid in terms of database structure - ie there are no missing columns or the columns are of the wrong type etc. But, I don't want the query to be actually executed. 其次,我想确保查询在数据库结构方面是有效的-即没有丢失的列或列的类型错误等。但是,我不希望查询实际执行。 The aim of this is to do a quick validation before the process kicks off because the process takes several hours and one syntactical error can waste a day of someone's time. 这样做的目的是在过程开始之前进行快速验证,因为该过程要花费几个小时,而一个语法错误可能会浪费某人一天的时间。

I will probably write the tool in C# with .net but if there's a pre-built tool that would be even better. 我可能会使用.net用C#编写该工具,但是如果有一个预先构建的工具会更好。 I will probably use the Access API. 我可能会使用Access API。 In SQL Server this is very straight forward. 在SQL Server中,这非常简单。 You can just validate the query in SQL Server management studio before running it. 您可以在运行查询之前先在SQL Server Management Studio中对其进行验证。 It will give you a good indication of whether the SQL will complete or not. 它会很好地指示SQL是否将完成。

How would I go about doing this? 我将如何去做呢?

Edit: an answer below solves the issue of checking syntax. 编辑:下面的答案解决了检查语法的问题。 However, I'd still like to be able to validate the semantic content of the query is OK. 但是,我仍然希望能够验证查询的语义内容是否正常。 However, I think this might be impossible in Access without actually running the query. 但是,我认为如果没有实际运行查询,这在Access中可能是不可能的。 Please tell me I'm wrong. 请告诉我我错了。

I'm not 100% sure if Access works the same way as a traditional database, but with a mainstream RDMBS, there are actually three distinct steps that happen when you run a query: 我不确定100%Access是否能以与传统数据库相同的方式工作,但是对于主流RDMBS,运行查询时实际上发生了三个不同的步骤:

  • Prepare 准备
  • Execute 执行
  • Fetch

Most are oblivious to the distinction because they just hit "run" and see results come back. 大多数人没有注意到这个区别,因为他们只是点击“运行”,然后看到结果返回。

It's the "Execute" that actually compiles the statement before going off and pulling data. 实际上是在关闭并提取数据之前编译语句的“ Execute”。

When you use ADO, you can actually see the three events as three separate calls to the database. 使用ADO时,实际上可以将这三个事件视为对数据库的三个单独调用。 What this means is you can trap the execute step to see if it fails, and if it succeeds, there is nothing requiring you to actually get the results. 这意味着您可以捕获执行步骤以查看它是否失败,如果成功,则没有什么要求您实际获得结果。

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = String.Format("{0}{1}",
    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=", @"c:\Access\MyDb.accdb");
conn.Open();

bool valid;
using (OleDbCommand cmd = new OleDbCommand("select [Bad Field] from [Table]", conn))
{
    try
    {
        OleDbDataReader reader = cmd.ExecuteReader();
        valid = true;
        reader.Close();   // Did not ever call reader.Read()
    }
    catch (Exception ex)
    {
        valid = false;
    }
}

And now valid indicates whether or not the statement compiled. 现在valid指示该语句是否已编译。

If you want to get really fancy, you can parse the exception results to find out why the command failed. 如果您真的想花哨的话,可以分析异常结果以找出命令失败的原因。

Access supports transactions on its Connection object. Access支持其Connection对象上的事务。 Try to execute your SQL statement inside a transaction and always call Rollback. 尝试在事务内执行SQL语句,并始终调用回滚。 Wrap the whole attempt in a Try/Catch block to assess whether the statement executed successfully or not. 将整个尝试包装在Try / Catch块中,以评估语句是否成功执行。

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

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