存储过程不使用.ASMX文件

时间:2013-06-24 11:24:37

标签: c# sql-server-2005 .net-3.5 asmx asp.net-3.5

任何人都知道为什么以下不起作用:

[WebMethod()]
public double GetDayCount(string strMeetingDate, string strMeetingTime)
{
    string[] strStartDateParts = strMeetingDate.Split('-');

    // change DMY to YMD
    strMeetingDate = strStartDateParts[2] + '-' + strStartDateParts[1] + '-' + strStartDateParts[0];

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {

        using (command = new SqlCommand("BusinessHours", connection))
        {

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@meeting_day", SqlDbType.DateTime).Value = strMeetingDate;
            command.Parameters.Add("@meeting_time", SqlDbType.DateTime).Value = strMeetingTime;

            connection.Open();

            using (reader = command.ExecuteReader())
            {
                reader.Read();
                return (double)reader["hours"];
            }
        }
    }
}

不会返回任何错误消息,而是返回的值不正确。


这是一个完整的存储过程。

USE [INTRANET]
GO
/****** Object:  StoredProcedure [dbo].[BusinessHours]    Script Date: 06/24/2013 11:38:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[BusinessHours]

@meeting_day DATETIME,
@meeting_time DATETIME

AS

DECLARE @submit_day DATETIME;
DECLARE @submit_time DATETIME;

DECLARE @start_time_of_business_day DATETIME;
DECLARE @business_day_hours FLOAT;

DECLARE @num1 FLOAT
DECLARE @num2 FLOAT
DECLARE @num3 FLOAT

SET @submit_day = CONVERT(VARCHAR(10),GETDATE(),101);
SET @submit_time =  CONVERT(VARCHAR(8),GETDATE(),108);

SET @start_time_of_business_day = '09:00';
SET @business_day_hours = 8.5;

SET @num1 = ((DATEDIFF(dd, @submit_day, @meeting_day))
-(DATEDIFF(wk, @submit_day, @meeting_day) * 2)
-(CASE WHEN DATEPART(dw, @submit_day) = 1 THEN 1 ELSE 0 END)
-(CASE WHEN DATEPART(dw, @meeting_day) = 7 THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM bank_holiday WHERE the_date BETWEEN @submit_day AND @meeting_day)) * @business_day_hours
SET @num2 = (select datediff(minute, @start_time_of_business_day, @submit_time)) / 60.0
SET @num3 = (select datediff(minute, @start_time_of_business_day, @meeting_time)) / 60.0

select @num1 - @num2 + @num3 as [hours]

如果我手动运行存储过程(如通过单击存储过程名称并选择执行从sql server管理工作室执行它),我得到0.666667(值将根据当前日期/时间和日期而不同/时间输入存储过程)。但是当我运行.asmx文件时,我得到994728.666667。

任何人都知道为什么会这样吗?基本上,.666667部分在两个示例中都是正确的,但由于某种原因,.asmx文件似乎将994728添加到正确的值。

2 个答案:

答案 0 :(得分:1)

您将字符串作为DateTime参数传递。以下应该有效:

[WebMethod()]
public double GetDayCount(string strMeetingDate, string strMeetingTime)
{
    string[] strStartDateParts = strMeetingDate.Split('-');
    // not sure what your expected time format is
    string[] srtStartTimeParts = strMeetingTime.Split('-');

    int year = Int32.Parse(strStartDateParts[2]);
    int month = Int32.Parse(strStartDateParts[1]);
    int day = Int32.Parse(strStartDateParts[0]);
    int hour = Int32.Parse(srtStartTimeParts[0]);
    int min = Int32.Parse(srtStartTimeParts[1]);
    int sec = Int32.Parse(srtStartTimeParts[2]);

    DateTime meetingDate = new DateTime(year, month, day, hour, min, sec);
     using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
         using (command = new SqlCommand("BusinessHours", connection))
        {
             command.CommandType = CommandType.StoredProcedure;
             command.Parameters.Add("@meeting_date", SqlDbType.DateTime).Value = meetingDate;
             connection.Open();
             using (reader = command.ExecuteReader())
            {
                reader.Read();
                return (double)reader["hours"];
            }
        }
    }
}

然后更改你的程序员,从你的论证中解析出日期和时间:

ALTER PROCEDURE [dbo].[BusinessHours]

@meeting_date DATETIME

AS

DECLARE @meeting_day DATETIME
DECLARE @meeting_time DATETIME
DECLARE @submit_day DATETIME;
DECLARE @submit_time DATETIME;

DECLARE @start_time_of_business_day DATETIME;
DECLARE @business_day_hours FLOAT;

DECLARE @num1 FLOAT
DECLARE @num2 FLOAT
DECLARE @num3 FLOAT

SET @meeting_day = CONVERT(VARCHAR(10),@meeting_date,101);
SET @meeting_time =  CONVERT(VARCHAR(8),@meeting_date,108);

SET @submit_day = CONVERT(VARCHAR(10),GETDATE(),101);
SET @submit_time =  CONVERT(VARCHAR(8),GETDATE(),108);

SET @start_time_of_business_day = '09:00';
SET @business_day_hours = 8.5;

SET @num1 = ((DATEDIFF(dd, @submit_day, @meeting_day))
-(DATEDIFF(wk, @submit_day, @meeting_day) * 2)
-(CASE WHEN DATEPART(dw, @submit_day) = 1 THEN 1 ELSE 0 END)
-(CASE WHEN DATEPART(dw, @meeting_day) = 7 THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM bank_holiday WHERE the_date BETWEEN @submit_day AND @meeting_day)) * @business_day_hours
SET @num2 = (select datediff(minute, @start_time_of_business_day, @submit_time)) / 60.0
SET @num3 = (select datediff(minute, @start_time_of_business_day, @meeting_time)) / 60.0

select @num1 - @num2 + @num3 as [hours]

答案 1 :(得分:0)

您可以启动SQL Server Profiler并亲自查看传递给您的过程的参数。

引用answer“SQL Server如何确定隐式日期时间转换的格式?”

“这可能取决于多种因素 - 操作系统的区域设置,当前用户的语言和日期格式设置。默认情况下,Windows使用美国英语,用户的设置为美国英语和MDY。”

“你最好的选择是,使用ISO标准,非区域,安全,明确的日期格式,如YYYYMMDD表示日期,YYYY-MM-DDTHH:MM:SS [.mmm]表示日期+时间。”< / p>

如果可能,我会在您的Web服务界面中将字符串更改为DateTime并检查sql跟踪以确保该调用与您从管理工作室手动执行的操作相对应。