我有一个VS2010 .Net 4.0 Web服务,它应该返回一个DataTable记录。它似乎一直很好,直到今天。如果我通过SQL Server Management Studio将5个参数(3个int和2个日期时间)传递给它,我会得到所有的行。但是,当我通过服务运行并检查ds.Fill(MyTable)行上的变量时,某些行不显示。
我刚注意到的奇怪之处,当我使用TagNumber 1-270搜索项目时,我得到了SSMS和.Fill中的所有行。每当我尝试返回TagNumbers在271和300之间的项目时,所有这些行都显示在SSMS中,但它们都没有返回到.Fill中。我检查过的所有东西都超过了300个,并且都符合预期。
这是我存储的程序。我不能说特别的。
ALTER PROCEDURE [dbo].[FetchLicenseReceiptData] (
@ForYear INT,
@StartNumber INT,
@EndNumber INT,
@StartDate DATETIME,
@EndDate DATETIME
)
AS
BEGIN
-- Also update udf GetLicenseReceiptData
SELECT
DISTINCT
-- License Fields
l.BarCode,
l.TagNumber,
l.Issued,
l.ForYear,
l.Fee,
l.WebOrderNumber,
lts.Name AS TagStyle,
CAST(CAST((l.ForYear + 1) AS VARCHAR) + '-01-31' AS DATETIME) AS RenewalDate,
-- Dog Fields
d.Name AS DogName,
d.BirthDate,
d.SpayNeuter,
d.Deceased,
lb.Name AS Breed,
lhl.Name AS HairLength,
lpc.Name AS PrimaryColor,
lsc.Name AS SecondaryColor,
lx.Name AS Sex,
-- Owner Fields
lt.Name AS Title,
o.FirstName,
o.Initial,
o.LastName,
-- Address Fields
a.Address1,
a.Address2,
a.City,
a.County,
ls.Abbreviation AS State,
a.ZipCode,
-- Contact Fields
jpi.PhoneNumber AS PrimaryPhoneNumber,
jpi.PhoneType AS PrimaryPhoneType,
jei.EmailAddress AS PrimaryEmail,
jei.EmailType AS PrimaryEmailType
FROM
data.License AS l
LEFT OUTER JOIN data.Dog AS d ON l.DogID = d.ID
LEFT OUTER JOIN data.Owner AS o ON d.OwnerID = o.ID
LEFT OUTER JOIN data.Address AS a ON o.AddressID = a.ID
LEFT OUTER JOIN lookup.Title AS lt ON o.TitleID = lt.ID
LEFT OUTER JOIN lookup.State AS ls ON a.StateID = ls.ID
LEFT OUTER JOIN lookup.TagStyle AS lts ON l.TagStyleID = lts.ID
LEFT OUTER JOIN lookup.Breed AS lb ON d.BreedID = lb.ID
LEFT OUTER JOIN lookup.HairLength AS lhl ON d.HairLengthID = lhl.ID
LEFT OUTER JOIN lookup.Color AS lpc ON d.PrimaryColorID = lpc.ID
LEFT OUTER JOIN lookup.Color AS lsc ON d.SecondaryColorID = lsc.ID
LEFT OUTER JOIN lookup.Sex AS lx ON d.SexID = lx.ID
LEFT OUTER JOIN (
SELECT
p.Number AS PhoneNumber,
lpt.Name AS PhoneType,
op.OwnerID AS OwnerID
FROM
data.OwnerPhone AS op
LEFT OUTER JOIN data.Phone AS p ON op.PhoneID = p.ID
LEFT OUTER JOIN lookup.PhoneType AS lpt ON p.PhoneTypeID = lpt.ID
WHERE
p.IsPrimary = 1
) AS jpi ON o.ID = jpi.OwnerID
LEFT OUTER JOIN (
SELECT
e.Address AS EmailAddress,
let.Name AS EmailType,
oe.OwnerID AS OwnerID
FROM
data.OwnerEmail AS oe
LEFT OUTER JOIN data.Email AS e ON oe.EmailID = e.ID
LEFT OUTER JOIN lookup.EmailType AS let ON e.EmailTypeID = let.ID
WHERE
e.IsPrimary = 1
) AS jei ON o.ID = jei.OwnerID
WHERE
l.ForYear = @ForYear
AND l.TagNumber >= @StartNumber
AND l.TagNumber <= @EndNumber
AND l.Issued >= @StartDate
AND l.Issued <= @EndDate
ORDER BY
l.TagNumber
END
这里结束的是我调用SP调用的方法:
public DataTable FetchLicenseReceiptData(int ForYear, int StartNumber, int EndNumber, DateTime StartDate, DateTime EndDate) {
DataTable Result = new DataTable();
try {
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DogLicensingContext"].ConnectionString)) {
SqlCommand cmd = new SqlCommand("FetchLicenseReceiptData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ForYear", ForYear));
cmd.Parameters.Add(new SqlParameter("@StartNumber", StartNumber));
cmd.Parameters.Add(new SqlParameter("@EndNumber", EndNumber));
cmd.Parameters.Add(new SqlParameter("@StartDate", StartDate));
cmd.Parameters.Add(new SqlParameter("@EndDate", EndDate));
con.Open();
SqlDataAdapter ds = new SqlDataAdapter(cmd);
ds.Fill(Result);
Result.TableName = "LicenseReceiptDataAutoPrint";
} // using the connection
} catch (Exception e) {
} // try-catch
return Result;
} // FetchLicenseReceiptData - Method
所以在我运行以下内容的SSMS中:
FetchLicenseReceiptData 2013,270,271,'2012-01-01','2012-12-31'
我得到2条记录。当通过.Fill运行我只得到一个(270)
当我在SSMS中运行以下内容时:
FetchLicenseReceiptData 2013,271,300,'2012-01-01','2012-12-31'
我得到了所有30行。但是通过.Fill它没有返回。
最后,如果我运行以下内容:
FetchLicenseReceiptData 2013,301,310,'2012-01-01','2012-12-31'
我在SSMS和.Fill都获得全部10分。
作为返回数据的示例,这里是SSMS中返回的270和271数据。我用波形符(〜)字符
替换了标签01CA2013T000270~270~2012-12-12 16:10:58.273~2013~20.00~NULL~Standard~2014-01-31 00:00:00.000~Theo~2008-02-13 00:00:00.000~ 0~0~Shar Pei~短〜杏〜白〜男〜未知~cccccccc ~~ kkkkkkk~4444 RICHLAND AVE。~~ xxxxxx ~~ OH~44444~5555555~Home~NULL~NULL
01CA2013T000271~271~2012-12-13 08:44:58.670~2013~20.00~NULL~Standard~2014-01-31 00:00:00.000~SAMSON~1999-12-12 00:00:00.000~ 1~0~拉布拉多犬〜中〜黑〜未知〜男〜女~~ xxxxx ~~ xxxxxxxxxx~4444 GIEL AVE ~~ xxxxxx ~~ OH~44444~5555555~Home~xxxxxxx~Personal
这个很奇怪。希望你们中的一个大师可以帮助我。
解: 感谢@EJBrennan提供的SQL Profiler建议。我之前从未使用它,但它指出了我正确的方向。这是实际上被发送到SQL:
exec FetchLicenseReceiptData @ ForYear = 2013,@ StartNumber = 270,@ EndNumber = 271,@ StartDate ='2012-01-01 00:00:00',@ EndDate ='2012-12-13 00:00:00 “
当我运行它时,它给了我一行而不是2.基本上,当时00:00:00的值是相同的除外。我最后将以下内容添加到我的存储过程的顶部以强迫事情正确:
SET @StartDate = DATEADD(DAY, DATEDIFF(DAY, 0, @StartDate), 0) -- Beginning of given Date
SET @EndDate = DATEADD(DAY, DATEDIFF(DAY, 0, @EndDate), 1) -- End of given Date
这会传递任何传入的@StartDate并强制它到当天的0:0:0。然后它将@EndDate设置为NEXT日,并将时间设置为0:0:0。
同样在WHERE子句中SP的底部,我将&lt; = @EndDate更改为&lt; EndDate从现在开始,@ EndDate是在日期过后的一天。
再次感谢@EJBrennan