简体   繁体   English

SQL Server ADO.Net从SQL脚本检索错误信息

[英]SQL Server ADO.Net retrieve error information from sql script

I am working on a project where we have to query an old SQL Server 2000 database (PeopleSoft, if you must know). 我正在做一个项目,在该项目中,我们必须查询旧的SQL Server 2000数据库(PeopleSoft,如果您必须知道的话)。 The query returns a resultset. 查询返回结果集。 If we were on a newer database, I would use stored procedures and error handling (eg @ErrNo, @ErrMsg, Try/Catch) to get what I want. 如果我们使用的是较新的数据库,我将使用存储过程和错误处理(例如@ ErrNo,@ ErrMsg,Try / Catch)来获取所需的信息。

However, we are on SQL Server 2000 and we are NOT allowed to use stored procedures, so we are left with only using dynamic SQL queries and scripts. 但是,由于我们使用的是SQL Server 2000,因此不允许使用存储过程,因此只能使用动态SQL查询和脚本。 We are currently using ADO.Net (eg DbCommand and Execute methods on it) to get our resultset. 我们当前正在使用ADO.Net(例如DbCommand和Execute方法)来获取结果集。

What I'd like to do is put more error handling / validation in our sql scripts and return messages to our .Net code. 我想做的是在我们的sql脚本中添加更多的错误处理/验证,并将消息返回到我们的.Net代码。 (eg input value not valid, row not found, etc.) (例如,输入值无效,找不到行等)

Does anyone know how to do this under these contraints? 有谁知道如何在这些约束下做到这一点? If there are no easy ways to do this, can you recommend some alternatives? 如果没有简便的方法,您可以推荐一些替代方法吗? eg 例如

  1. ask the DBA to create a specialized message / logging table for us and in our scripts, write to that table and in our code, read that table 要求DBA为我们以及在脚本中创建专门的消息/日志记录表,写入该表和代码,然后读取该表

  2. break up our code / scripts into multiple steps and call it from our .Net code (would we then have to use transactions?) 将我们的代码/脚本分解成多个步骤,然后从.Net代码中调用它(然后我们将不得不使用事务吗?)

  3. etc. etc.? 等等等? Really banging my head here... 真是在敲我的头...

Thanks everyone. 感谢大家。

Stored procedures have little to do with what (I feel) you trying to achieve. 存储过程与您试图实现的目标无关(我认为)。 Even if you have Error handling in stored procedure, you still going to send invalid data to get error handled. 即使存储过程中有错误处理,您仍将发送无效数据以获取错误处理。 Seems, what you need is better "before DB call" validation. 似乎,您需要的是更好的“数据库调用之前”验证。

What you can do to improve your situation is to utilize Ado.net to great extent. 您可以做的以改善您的状况是在很大程度上利用Ado.net。 Specifically - use DataTables . 专门使用DataTables You can load schema and it will create your DataTable in shape of your Sql Table. 您可以加载架构,它将以Sql Table的形式创建DataTable Check the properties of the DataTable Column object http://msdn.microsoft.com/en-us/library/system.data.datacolumn_properties%28v=vs.90%29.aspx 检查DataTable Column对象的属性http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn_properties%28v=vs.90%29.aspx

If you try to insert invalid data into this table first, it will error out and you will be able to create specific error handling. 如果您尝试首先将无效数据插入该表中,它将出错,并且您将能够创建特定的错误处理。

Also, you can (and should) replace dynamic sql with parametrized query. 另外,您可以(并且应该)用参数化查询替换动态sql。 This is recommended way and will improve performance. 这是推荐的方法,可以提高性能。

Instead of : 代替 :

"select * from customer where id = " + custId.ToString()

You should do: 你应该做:

"select * from customer where id = @1"

Then, when you create your command object, you will add a parameter. 然后,当您创建命令对象时,将添加一个参数。 This is good for Sql Server performance. 这对于Sql Server性能是有好处的。

While for you, DataTable will be line of defense against sending erroneous code to server. 对于您来说, DataTable将是防止向服务器发送错误代码的防线。

Another way is writing entirely your own data validation logic based on INFORMATION_SCHEMA . 另一种方法是完全根据INFORMATION_SCHEMA编写自己的数据验证逻辑。

Another way is to have really strict data input validation. 另一种方法是进行严格的数据输入验证。

Normally, systems have both, input validation and data layer validation. 通常,系统同时具有输入验证和数据层验证。

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

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