为什么存储过程结果为零?

发布于 2024-10-31 20:36:48 字数 2928 浏览 1 评论 0原文

使用 C# 与 2008。我在 SQL Server 2005 中有一个存储过程。我使用 Linq-to-SQL 执行该存储过程,但结果为零。当我在 Sql 服务器中运行查询时,

 exec spGetInvoiceByDate '03/01/2011 00:00:00 AM','03/31/2011 11:59:59 PM'

上面的语法工作正常。

这是我的 SQL Server 存储过程

create procedure [spGetInvoiceByDate]  
  @Beginning_Date DateTime, @Ending_Date DateTime  
as  
begin  

SELECT * INTO #temp1 FROM (  
SELECT     convert(varchar, ManifestPort.StartDate, 1)+'-'+CASE dbo.ManifestPort.EndDate WHEN '1800-01-01 00:00:00.000' THEN 'N/A' ELSE convert(varchar, ManifestPort.EndDate, 1) END AS [Date], Manifest.VesselName, Manifest.VoyageNo, DischargePort.PortName
, ManifestPort.Terminal, COUNT(Seal.ContainerNo) TotalContainers, 0 AS TotalHours, 0 AS TotalFee  
FROM ManifestPort 
INNER JOIN Manifest ON ManifestPort.ManifestNo = Manifest.ManifestNo 
INNER JOIN DischargePort ON ManifestPort.PortCode = DischargePort.PortCode 
INNER JOIN Seal ON ManifestPort.ManifestPortNo = Seal.ManifestPortNo  
WHERE (StartDate BETWEEN @Beginning_Date AND @Ending_Date)                
GROUP BY StartDate,EndDate, VesselName, VoyageNo, PortName, Terminal) AS a  

SELECT * INTO #temp2 FROM (  
SELECT convert(varchar, ManifestPort.StartDate, 1)+'-'+CASE dbo.ManifestPort.EndDate WHEN '1800-01-01 00:00:00.000' THEN 'N/A' ELSE convert(varchar, ManifestPort.EndDate, 1) END AS [Date], Manifest.VesselName, Manifest.VoyageNo, DischargePort.PortName, 
ManifestPort.Terminal, COUNT(Seal.ContainerNo) STotalContainers, 0 AS STotalHours, 0 AS STotalFee  
FROM ManifestPort 
INNER JOIN Manifest ON ManifestPort.ManifestNo = Manifest.ManifestNo 
INNER JOIN DischargePort ON ManifestPort.PortCode = DischargePort.PortCode 
INNER JOIN Seal ON ManifestPort.ManifestPortNo = Seal.ManifestPortNo  
WHERE (StartDate BETWEEN @Beginning_Date AND @Ending_Date ) AND Line = 'CSAV'  
GROUP BY StartDate,EndDate, VesselName, VoyageNo, PortName, Terminal     ) AS b  

SELECT  #temp1.Date,  
        #temp1.VesselName ,  
        #temp1.VoyageNo ,  
        #temp1.PortName ,  
        #temp1.Terminal ,  
        #temp1.TotalContainers ,  
        #temp1.TotalHours ,  
        #temp1.TotalFee, #temp2.STotalContainers, #temp2.STotalHours, #temp2.STotalFee FROM #temp1 INNER JOIN #temp2  
        ON #temp1.Date = #temp2.Date AND #temp1.PortName = #temp2.PortName AND #temp1.Terminal = #temp2.Terminal AND #temp1.VesselName = #temp2.VesselName  
        AND #temp1.VoyageNo = #temp2.VoyageNo  

        Order BY #temp1.VesselName, #temp1.VoyageNo  

DROP TABLE #temp1  
DROP TABLE #temp2  

end

我使用这个 C# Linq 语法 来执行存储过程:

DateTime firstDayOfMonth = Convert.ToDateTime("03/01/2011");
DateTime lastDayOfMonth = Convert.ToDateTime("03/31/2011");

var results = ProviderName.spGetInvoiceByDate(firstDayOfMonth,lastDayOfMonth);

为什么我总是得到结果为零?

谁能告诉我如何减少这个问题?

预先感谢,如果您有任何疑问,请询问。

Work on C# vs 2008. I have a stored procedure in SQL Server 2005. Using Linq-to-SQL I execute the stored procedure, but the result is zero. When i run the query in Sql server it's work fine

 exec spGetInvoiceByDate '03/01/2011 00:00:00 AM','03/31/2011 11:59:59 PM'

above syntax works fine.

Here is my SQL Server stored procedure

create procedure [spGetInvoiceByDate]  
  @Beginning_Date DateTime, @Ending_Date DateTime  
as  
begin  

SELECT * INTO #temp1 FROM (  
SELECT     convert(varchar, ManifestPort.StartDate, 1)+'-'+CASE dbo.ManifestPort.EndDate WHEN '1800-01-01 00:00:00.000' THEN 'N/A' ELSE convert(varchar, ManifestPort.EndDate, 1) END AS [Date], Manifest.VesselName, Manifest.VoyageNo, DischargePort.PortName
, ManifestPort.Terminal, COUNT(Seal.ContainerNo) TotalContainers, 0 AS TotalHours, 0 AS TotalFee  
FROM ManifestPort 
INNER JOIN Manifest ON ManifestPort.ManifestNo = Manifest.ManifestNo 
INNER JOIN DischargePort ON ManifestPort.PortCode = DischargePort.PortCode 
INNER JOIN Seal ON ManifestPort.ManifestPortNo = Seal.ManifestPortNo  
WHERE (StartDate BETWEEN @Beginning_Date AND @Ending_Date)                
GROUP BY StartDate,EndDate, VesselName, VoyageNo, PortName, Terminal) AS a  

SELECT * INTO #temp2 FROM (  
SELECT convert(varchar, ManifestPort.StartDate, 1)+'-'+CASE dbo.ManifestPort.EndDate WHEN '1800-01-01 00:00:00.000' THEN 'N/A' ELSE convert(varchar, ManifestPort.EndDate, 1) END AS [Date], Manifest.VesselName, Manifest.VoyageNo, DischargePort.PortName, 
ManifestPort.Terminal, COUNT(Seal.ContainerNo) STotalContainers, 0 AS STotalHours, 0 AS STotalFee  
FROM ManifestPort 
INNER JOIN Manifest ON ManifestPort.ManifestNo = Manifest.ManifestNo 
INNER JOIN DischargePort ON ManifestPort.PortCode = DischargePort.PortCode 
INNER JOIN Seal ON ManifestPort.ManifestPortNo = Seal.ManifestPortNo  
WHERE (StartDate BETWEEN @Beginning_Date AND @Ending_Date ) AND Line = 'CSAV'  
GROUP BY StartDate,EndDate, VesselName, VoyageNo, PortName, Terminal     ) AS b  

SELECT  #temp1.Date,  
        #temp1.VesselName ,  
        #temp1.VoyageNo ,  
        #temp1.PortName ,  
        #temp1.Terminal ,  
        #temp1.TotalContainers ,  
        #temp1.TotalHours ,  
        #temp1.TotalFee, #temp2.STotalContainers, #temp2.STotalHours, #temp2.STotalFee FROM #temp1 INNER JOIN #temp2  
        ON #temp1.Date = #temp2.Date AND #temp1.PortName = #temp2.PortName AND #temp1.Terminal = #temp2.Terminal AND #temp1.VesselName = #temp2.VesselName  
        AND #temp1.VoyageNo = #temp2.VoyageNo  

        Order BY #temp1.VesselName, #temp1.VoyageNo  

DROP TABLE #temp1  
DROP TABLE #temp2  

end

I use this C# Linq syntax to execute the stored procedure:

DateTime firstDayOfMonth = Convert.ToDateTime("03/01/2011");
DateTime lastDayOfMonth = Convert.ToDateTime("03/31/2011");

var results = ProviderName.spGetInvoiceByDate(firstDayOfMonth,lastDayOfMonth);

Why do I always get result is zero?

Can anybody tell me how to reduce the problem?

Thanks in advance, if you have any question, please ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩于岁月 2024-11-07 20:36:48

您是否尝试过通过将 @Beginning_Date 和 @Ending_Date 替换为传入 C# Linq 代码的值来执行存储过程(在 SQL Server Management Studio 上)?
它返回任何结果吗?
如果它也是 0,那就解释了为什么您的 Linq 代码返回 0。

希望有帮助。

Have you tried executing the stored proc (on SQL Server Management Studio) by replacing @Beginning_Date and @Ending_Date with the values you pass in to your C# Linq code?
Does it return any result?
If it's also 0, that explains why your Linq code returns 0.

Hope that helps.

预谋 2024-11-07 20:36:48

正如 Hans Kesting 上面提到的,在 SQL 查询中调用存储过程与通过代码调用时,您没有使用相同的日期/时间。

exec spGetInvoiceByDate '03/01/2011 00:00:00 AM','03/31/2011 11:59:59 PM'

将代码中的结束日期设置为 2011 年 3 月 31 日晚上 11:59:59。

DateTime firstDayOfMonth = Convert.ToDateTime("03/01/2011");
DateTime lastDayOfMonth = Convert.ToDateTime("03/31/2011 11:59:59 PM");

var results = ProviderName.spGetInvoiceByDate(firstDayOfMonth,lastDayOfMonth);

As mentioned above by Hans Kesting, you're not using the same date/times in your call to the stored procedure in your SQL query vs. your call via the code.

exec spGetInvoiceByDate '03/01/2011 00:00:00 AM','03/31/2011 11:59:59 PM'

Set the end date in the code to March 31 2011, 11:59:59 PM.

DateTime firstDayOfMonth = Convert.ToDateTime("03/01/2011");
DateTime lastDayOfMonth = Convert.ToDateTime("03/31/2011 11:59:59 PM");

var results = ProviderName.spGetInvoiceByDate(firstDayOfMonth,lastDayOfMonth);
策马西风 2024-11-07 20:36:48

Linq to SQL 不支持基于临时表生成结果的存储过程的代码生成。请参阅 http://msdn.microsoft 中的 LINQ to SQL 对存储过程的限制部分。 com/en-us/library/bb425822.aspx 了解更多详细信息。解决此问题的一种方法是暂时将存储过程更改为使用普通表而不是临时表,在 Visual Studio 中生成 Linq to Sql 代码,然后将存储过程更改回使用临时表。删除在存储过程的临时版本中创建的任何不需要的表。

Linq to SQL does not support code generation for stored procedures that produce results based on temporary tables. See the LINQ to SQL Limitations on Stored Procedures section in http://msdn.microsoft.com/en-us/library/bb425822.aspx for more details. One way to get round this is to temporarily change your stored procedure to use normal tables instead of temporary tables, generate the Linq to Sql code in Visual Studio and then change your stored procedure back to use temporary tables. Delete any unwanted tables created in the temporary version of the stored procedure.

墨离汐 2024-11-07 20:36:48

您在此处的代码中遗漏了一些内容...

通常,单个整数的结果意味着您正在获取过程的返回代码,该返回代码用于指示发生错误时的错误状态(0 表示全部为错误状态)好吧)而你没有得到结果集。

您能否发布 ProviderName.spGetInvoiceByDate( 方法的代码(即使它只是反映)?我敢打赌它在内部做了一些我们不知道的事情。

There's something you're leaving out in the code here...

Normally a result of a single integer implies that you're getting the return code of the procedure, which is used to indicate an error state if one occurred (0 means all was well) and you're not getting the resultset.

Can you post the code (even if it is just reflected) of the ProviderName.spGetInvoiceByDate( method? I'm willing to bet that it does something internally that we're not aware of.

走过海棠暮 2024-11-07 20:36:48

我猜这是由于日期格式不匹配造成的。请验证 C#SQL Server 中参数的日期格式是否相同。如果不匹配,您可以在存储过程本身中转换格式。

I guess this is due to mismatch in the Date Format. Please verify that the date format of parameters are same in C# as well as SQL Server. In case of mismatch you can convert the format in Stored Procedure itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文