简体   繁体   English

从 C# Windows 应用程序调用存储过程的错误结果

[英]Wrong result calling stored procedure from C# Windows application

I have a weird issue that I don't know where to begin to look to try to resolve it.我有一个奇怪的问题,我不知道从哪里开始尝试解决它。 I am writing a Windows application with a SQL Server back end.我正在使用 SQL 服务器后端编写 Windows 应用程序。 I've written a stored procedure that the application calls and then outputs the results into an Excel spreadsheet.我编写了一个应用程序调用的存储过程,然后将结果输出到 Excel 电子表格中。

The issue is, one column's values are wrong.问题是,一列的值是错误的。 This column is a SUM value where the sum is done in the stored procedure.此列是一个 SUM 值,其中总和在存储过程中完成。 The weird thing is when I run the procedure in SQL Server Management Studio, the value returns correctly.奇怪的是,当我在 SQL Server Management Studio 中运行该程序时,该值正确返回。 When I call the same procedure from my Windows app, the values are wrong.当我从我的 Windows 应用程序中调用相同的程序时,这些值是错误的。 They are somewhat in the ballpark but wrong.他们有点在球场上,但错了。 Note that there are 14 columns returned and the other 13 are correct so I think it has to do with the aggregate function.请注意,返回了 14 列,其他 13 列是正确的,所以我认为这与聚合 function 有关。

I've stepped through the code to see the values starting with the 'while(reader.Read())' loop and can see that values are wrong before they are put into the spreadsheet.我已经通过代码查看了以“while(reader.Read())”循环开头的值,并且可以在将它们放入电子表格之前看到这些值是错误的。 Why are they correct in SQL but not in the Windows app?为什么它们在 SQL 中正确,但在 Windows 应用程序中不正确? It's the same code running.这是相同的代码运行。 Very weird.很奇怪。

If anybody has any thoughts on this I would appreciate them.如果有人对此有任何想法,我将不胜感激。 If it would help to post some code I'm happy to do that.如果发布一些代码会有所帮助,我很乐意这样做。

Thanks in advance.提前致谢。

You can try to execute the stored procedure with SqlAdapter to return a data set and check if the data type of the Aggregated(SUM) column is equivalent to the return type from the DataReader that you had a problem with.您可以尝试使用 SqlAdapter 执行存储过程以返回数据集,并检查 Aggregated(SUM) 列的数据类型是否等同于您遇到问题的 DataReader 的返回类型。

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

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