- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有几个存储过程读取同一个表并以相同的布局返回数据。这些存储过程之一导致存储为位的字段出现无效强制转换异常。 SqlDataReader
认为它是一个 int32
。
示例表
create table dbo.SampleTable
(
Id INT NOT NULL IDENTITY(1,1),
SomeText VARCHAR(40),
BoolValue BIT
)
示例存储过程
create proc SampleProcedure
as
select Id,
SomeText,
BoolValue
go
示例扩展类
using System;
using System.Data.SqlClient;
namespace SampleApp.Extensions{
public static class SampleModelExtension{
private const int INDEX_ID = 0;
private const int INDEX_SOMETEXT = 1;
private const int INDEX_BOOLVALUE = 2;
public static SampleModel ToSampleModel(this SqlDataReader rdr){
SampleModel myModel = new SampleModel();
myModel.Id = !rdr.IsDbNull(INDEX_ID) ? rdr.GetInt32(INDEX_ID) : 0;
myModel.SomeText = !rdr.IsDbNull(INDEX_SOMETEXT) ? rdr.GetString(INDEX_SOMETEXT) : String.Empty;
myModel.Boolvalue = !rdr.IsDbNull(INDEX_BOOLVALUE) ? rdr.GetBool(INDEX_BOOLVALUE) : false;
return myModel;
}
}
}
示例存储库类
using SampleApp.Extensions;
using System;
using System.Collections.Generic;
using System.Data.SqlCient;
namespace SampleApp {
public SampleRepository : BaseDataConnection {
public List<SampleModel> GetSampleData(){
SqlCommand cmd = new SqlCommand("SampleProcedure", base.Connection);
List<SampleModel> retVal = new List<SampleModel>();
using(SqlDataReader rdr = base.GetDataReader(cmd)){
while(rdr.Read()){
retVal.Add(rdr.ToSampleModel());
}
}
return retVal;
}
public List<SampleModel> GetMoreSampleData(){
SqlCommand cmd = new SqlCommand("AnotherSampleProcedure", base.Connection);
List<SampleModel> retVal = new List<SampleModel>();
using(SqlDataReader rdr = base.GetDataReader(cmd)){
while(rdr.Read()){
retVal.Add(rdr.ToSampleModel());
}
}
return retVal;
}
}
}
这与我的设置类似。在我的代码中,我有一个扩展方法,可以将 SqlDataReader
转换为 SampleModel
的类型,以便在存储库类中的所有加载方法中重用该扩展方法。正是因为这种方法,我知道它可以与所有其他方法一起使用。
关于为什么它将列视为 int 而不是 bit 有什么想法吗?
实际存储过程
ALTER PROCEDURE [dbo].[GetAllActiveScheduledEventsByDateRange]
@SiteGuid VARCHAR(38),
@StartDate DATE,
@EndDate DATE
AS
SELECT se.EventId,
se.AvailableDate,
se.StartTime,
se.NumberOfPatrons,
se.AgeOfPatrons,
se.ContactEmailAddress,
se.ContactPhone,
se.ContactName,
se.EventTypeId,
se.PartyName,
se.ConfirmationDateTime,
se.ReminderDateTime,
se.UserComments,
se.AdminComments,
se.Active,
se.CheckInTime,
se.CheckOutTime,
se.GunSize,
(
Select Count(p.playerid) from
(select * from waiver2 where waiverid in (
(Select WaiverId
from Waiver2
inner join
(
Select max(CreateDateTime) as LatestDate, PlayerId
from Waiver2
WHERE siteguid = @SiteGuid
Group by PlayerId
) SubMax
on Waiver2.CreateDateTime = SubMax.LatestDate
and Waiver2.PlayerId = SubMax.PlayerId))) w,
player p, PlayDateTime updt
where p.playerid = w.playerid
and p.playerid = updt.PlayerId
and updt.EventId = se.EventId) AS WaiverCount,
se.DepositAmount,
se.CreateDateTime,
se.PaymentReminderDateTime,
se.PaymentStatusId,
se.PackageId
FROM ScheduledEvent se
WHERE se.SiteGuid = @SiteGuid
AND se.AvailableDate BETWEEN @StartDate AND @EndDate
AND se.PaymentStatusId < '99'
AND se.Active = 1
ORDER BY se.StartTime, se.ContactName
Active
列是引发错误的列。它被定义为 BIT
并作为第 14 列进行索引。
导致问题的实际存储过程
ALTER proc [dbo].[W2_GetAllActiveScheduledEventsByDateWithWaivers]
@SiteGuid VARCHAR(38),
@AvailableDate DATE
AS
SELECT se.EventId,
se.AvailableDate,
se.StartTime,
se.NumberOfPatrons,
se.AgeOfPatrons,
se.ContactEmailAddress,
se.ContactPhone,
se.ContactName,
se.EventTypeId,
se.PartyName,
se.ConfirmationDateTime,
se.ReminderDateTime,
se.UserComments,
se.AdminComments,
se.Active,
se.CheckInTime,
se.CheckOutTime,
se.GunSize,
(
Select Count(p.playerid) from
(
select * from waiver2 where waiverid in
(
(
Select WaiverId
from Waiver2
inner join
(
Select max(CreateDateTime) as LatestDate, PlayerId
from Waiver2
WHERE siteguid = @SiteGuid
Group by PlayerId
) SubMax
on Waiver2.CreateDateTime = SubMax.LatestDate
and Waiver2.PlayerId = SubMax.PlayerId
and DateDiff(year,Waiver2.CreateDateTime,GETDATE()) = 0
)
)
) w,
player p, PlayDateTime updt
where p.playerid = w.playerid
and p.playerid = updt.PlayerId
and updt.EventId = se.EventId
and ((
FLOOR(DATEDIFF(day,p.DateOfBirth,GETDATE())/365.242199) >= 18
and
w.ParentId is null
)
or
(
FLOOR(DATEDIFF(day,p.DateOfBirth,GETDATE())/365.242199) < 18
and
w.ParentId is not null
))
) AS WaiverCount,
se.DepositAmount,
se.CreateDateTime,
se.PaymentReminderDateTime,
se.PaymentStatusId,
se.PackageId
FROM ScheduledEvent se
WHERE se.SiteGuid = @SiteGuid
AND se.AvailableDate = @AvailableDate
AND se.PaymentStatusId <= '90'
AND se.Active = 1
--ORDER BY se.StartTime, se.ContactName
union select null,
pdt.PlayDate,
pdt.PlayTime,
null,
null,
null,
null,
null,
null,
'Walk-up Players',
null,
null,
null,
null,
1,
null,
null,
null,
COUNT('x') AS WaiverCount,
0,
null,
null,
null,
null
from PlayDateTime pdt
where pdt.PlayDate = @AvailableDate
and pdt.EventId is null
and pdt.PlayerId in (
Select p.playerid from
(select * from waiver2 where waiverid in (
(Select WaiverId
from Waiver2
inner join
(
Select max(CreateDateTime) as LatestDate, PlayerId
from Waiver2
WHERE siteguid = @SiteGuid
Group by PlayerId
) SubMax
on Waiver2.CreateDateTime = SubMax.LatestDate
and Waiver2.PlayerId = SubMax.PlayerId
and DateDiff(year,Waiver2.CreateDateTime,GETDATE()) = 0))) w,
player p
where p.playerid = w.playerid
and ((
FLOOR(DATEDIFF(day,p.DateOfBirth,GETDATE())/365.242199) >= 18
and
w.ParentId is null
)
or
(
FLOOR(DATEDIFF(day,p.DateOfBirth,GETDATE())/365.242199) < 18
and
w.ParentId is not null
))
)
group by pdt.PlayDate, pdt.PlayTime
order by 2, 3, 10
这是实际的扩展类(更改名称以保护无辜者)
namespace MyNameSpace.Svc.Core.Extensions.Registration {
public static class ScheduledEventExtension {
#region attributes
private const int INDEX_ID = 0;
private const int INDEX_DATE = 1;
private const int INDEX_STARTTIME = 2;
private const int INDEX_NUMBEROFPATRONS = 3;
private const int INDEX_AGEOFPATRONS = 4;
private const int INDEX_CONTACTEMAIL = 5;
private const int INDEX_CONTACTPHONE = 6;
private const int INDEX_CONTACTNAME = 7;
private const int INDEX_EVENTTYPE = 8;
private const int INDEX_PARTYNAME = 9;
private const int INDEX_CONFIRMDATE = 10;
private const int INDEX_REMINDDATE = 11;
private const int INDEX_USERCOMMENTS = 12;
private const int INDEX_ADMINCOMMENTS = 13;
private const int INDEX_ACTIVE = 14;
private const int INDEX_CHECKINTIME = 15;
private const int INDEX_CHECKOUTTIME = 16;
private const int INDEX_GUNSIZE = 17;
private const int INDEX_WAIVERCOUNT = 18;
private const int INDEX_DEPOSITAMOUNT = 19;
private const int INDEX_CREATEDATETIME = 20;
private const int INDEX_PAYMENTREMINDERDATETIME = 21;
private const int INDEX_PAYMENTSTATUS = 22;
private const int INDEX_PACKAGEID = 23;
#endregion
#region methods
public static ScheduledEvent ToScheduledEvent(this SqlDataReader rdr) {
ScheduledEvent retVal = new ScheduledEvent();
retVal.Id = !rdr.IsDBNull(INDEX_ID) ? rdr.GetInt32(INDEX_ID) : 0;
retVal.SelectedDate.SelectedDate = !rdr.IsDBNull(INDEX_DATE) ? rdr.GetDateTime(INDEX_DATE) : DateTime.MinValue;
retVal.SelectedDate.StartTime = !rdr.IsDBNull(INDEX_STARTTIME) ? rdr.GetTimeSpan(INDEX_STARTTIME) : TimeSpan.MinValue;
int numOfPatrons = 0;
int.TryParse(rdr.GetString(INDEX_NUMBEROFPATRONS), out numOfPatrons);
retVal.NumberOfPatrons = numOfPatrons;
retVal.AgeOfPatrons = !rdr.IsDBNull(INDEX_AGEOFPATRONS) ? rdr.GetString(INDEX_AGEOFPATRONS) : string.Empty;
retVal.ContactEmailAddress = !rdr.IsDBNull(INDEX_CONTACTEMAIL) ? rdr.GetString(INDEX_CONTACTEMAIL) : string.Empty;
retVal.ContactPhone = !rdr.IsDBNull(INDEX_CONTACTPHONE) ? rdr.GetString(INDEX_CONTACTPHONE) : string.Empty;
retVal.ContactName = !rdr.IsDBNull(INDEX_CONTACTNAME) ? rdr.GetString(INDEX_CONTACTNAME) : string.Empty;
// event type is obsolete
retVal.PartyName = !rdr.IsDBNull(INDEX_PARTYNAME) ? rdr.GetString(INDEX_PARTYNAME) : string.Empty;
retVal.ConfirmationDateTime = !rdr.IsDBNull(INDEX_CONFIRMDATE) ? rdr.GetDateTime(INDEX_CONFIRMDATE) : DateTime.MinValue;
retVal.ReminderDateTime = !rdr.IsDBNull(INDEX_REMINDDATE) ? rdr.GetDateTime(INDEX_REMINDDATE) : DateTime.MinValue;
retVal.Comments = !rdr.IsDBNull(INDEX_USERCOMMENTS) ? rdr.GetString(INDEX_USERCOMMENTS) : string.Empty;
retVal.AdminComments = !rdr.IsDBNull(INDEX_ADMINCOMMENTS) ? rdr.GetString(INDEX_ADMINCOMMENTS) : string.Empty;
retVal.Active = !rdr.IsDBNull(INDEX_ACTIVE) ? rdr.GetBoolean(INDEX_ACTIVE) : false;
retVal.CheckInDateTime = !rdr.IsDBNull(INDEX_CHECKINTIME) ? rdr.GetDateTime(INDEX_CHECKINTIME) : DateTime.MinValue;
retVal.CheckOoutDateTime = !rdr.IsDBNull(INDEX_CHECKOUTTIME) ? rdr.GetDateTime(INDEX_CHECKOUTTIME) : DateTime.MinValue;
// gun size is obsolete
retVal.WaiverCount = !rdr.IsDBNull(INDEX_WAIVERCOUNT) ? rdr.GetInt32(INDEX_WAIVERCOUNT) : 0;
retVal.DepositAmount = !rdr.IsDBNull(INDEX_DEPOSITAMOUNT) ? rdr.GetDecimal(INDEX_DEPOSITAMOUNT) : 0;
retVal.CreateDateTime = !rdr.IsDBNull(INDEX_CREATEDATETIME) ? rdr.GetDateTime(INDEX_CREATEDATETIME) : DateTime.MinValue;
retVal.PaymentReminderDateTime = !rdr.IsDBNull(INDEX_PAYMENTREMINDERDATETIME) ? rdr.GetDateTime(INDEX_PAYMENTREMINDERDATETIME) : DateTime.MinValue;
retVal.PaymentStatus = !rdr.IsDBNull(INDEX_PAYMENTSTATUS) ? PaymentStatusExtension.ToPaymentStatusEnum(rdr.GetString(INDEX_PAYMENTSTATUS)) : PaymentStatusEnum.Unpaid;
retVal.SelectedPackage.Id = !rdr.IsDBNull(INDEX_PACKAGEID) ? rdr.GetInt32(INDEX_PACKAGEID) : 0;
return retVal;
}
#endregion
}
}
这是我的存储库类(同样经过少量修改)
using MyNameSpace.Svc.Core.Extensions;
using MyNameSpace.Svc.Core.Extensions.Registration;
using MyNameSpace.Svc.Core.Interfaces.Registration;
using MyNameSpace.Svc.Core.Models.Registration;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace MyNameSpace.Svc.Impl.Repositories.Registration {
public class ScheduledEventRepositoryImpl : DatabaseConnection, IScheduledEventRepository {
#region attributes
private const string PARMNAME_RETURN = "retval";
private const string PARMNAME_ID = "EventId";
private const string PARMNAME_GUID = "SiteGuid";
private const string PARMNAME_AVAILABLEDATE = "AvailableDate";
private const string PARMNAME_STARTTIME = "StartTime";
private const string PARMNAME_NUMPATRONS = "NumberOfPatrons";
private const string PARMNAME_AGEPATRONS = "AgeOfPatrons";
private const string PARMNAME_CONTACTEMAIL = "ContactEmailAddress";
private const string PARMNAME_CONTACTPHONE = "ContactPhone";
private const string PARMNAME_CONTACTNAME = "ContactName";
private const string PARMNAME_PARTYNAME = "PartyName";
private const string PARMNAME_CONFDATE = "ConfirmationDateTime";
private const string PARMNAME_REMINDDATE = "ReminderDateTime";
private const string PARMNAME_USERCOMMENTS = "UserComments";
private const string PARMNAME_ADMINCOMMENTS = "AdminComments";
private const string PARMNAME_CHECKINTIME = "CheckInTime";
private const string PARMNAME_CHECKOUTTIME = "CheckOutTime";
private const string PARMNAME_DEPOSITAMT = "DepositAmount";
private const string PARMNAME_CREATEDATE = "CreateDateTime";
private const string PARMNAME_PAYMENTREMINDDATE = "PaymentReminderDateTime";
private const string PARMNAME_PAYMENTSTATUS = "PaymentStatusId";
private const string PARMNAME_PKGID = "PackageId";
private const string PARMNAME_EMAIL = "EmailAddress";
private const string PARMNAME_DAYSOUT = "DaysOut";
private const string PARMNAME_EVENTTYPE = "EventTypeId";
private const string PARMNAME_STARTDATE = "StartDate";
private const string PARMNAME_ENDDATE = "EndDate";
private const string SPNAME_GETALLACTIVEBYDATERANGE = "GetAllActiveScheduledEventsByDateRange";
private const string SPNAME_GETALLACTIVEBYDATEWITHWAIVERS = "W2_GetAllActiveScheduledEventsByDateWithWaivers";
#endregion
#region methods
public List<ScheduledEvent> GetAllActiveScheduledEventsByDateRange(Guid siteGuid, DateTime startDate, DateTime endDate) {
List<ScheduledEvent> retVal = new List<ScheduledEvent>();
SqlCommand cmd = new SqlCommand(SPNAME_GETALLACTIVEBYDATERANGE, base.Connection);
cmd.Parameters.AddWithValue(PARMNAME_GUID, siteGuid.ToFormattedString());
cmd.Parameters.AddWithValue(PARMNAME_STARTDATE, startDate);
cmd.Parameters.AddWithValue(PARMNAME_ENDDATE, endDate);
using(SqlDataReader rdr = base.GetDataReader(cmd)) {
while(rdr.Read()) {
retVal.Add(rdr.ToScheduledEvent());
}
}
return retVal;
}
public List<ScheduledEvent> GetAllActiveScheduledEventsByDateWithWaivers(Guid siteGuid, DateTime availableDate) {
List<ScheduledEvent> retVal = new List<ScheduledEvent>();
using(SqlDataReader rdr = base.GetDataReader(SPNAME_GETALLACTIVEBYDATEWITHWAIVERS, PARMNAME_AVAILABLEDATE, availableDate, siteGuid)) {
while(rdr.Read()) {
retVal.Add(rdr.ToScheduledEvent());
}
}
return retVal;
}
#endregion
}
}
最佳答案
高度怀疑“SqlDataReader
认为它是一个int32
”。 SqlDataReader
只知道每个字段是什么,因为 RDBMS 将结果集的模式与结果集一起发送。因此该列(或更准确地说:结果集中的那个字段)是一个 int
。
在最初的最后 3 个代码示例(标记为“实际”)中,存储过程在名称或参数方面与存储库类调用的过程不匹配。
现在代码示例已经更新,真正的过程(如问题评论中所述)有一个 UNION
,其中同一列有一个文字 1
被选中。文字 1
的默认类型是 INT
,因此 BIT
字段被隐式转换为 INT< 是有意义的
因为 UNION
。
如果您想实际查看此操作,只需尝试以下操作(通过 SQL Server 2012 中引入的 sys.dm_exec_describe_first_result_set 查看结果集的架构):
SELECT [system_type_name]
FROM sys.dm_exec_describe_first_result_set('SELECT CONVERT(BIT, 1) AS [BITorINT?]',
NULL, NULL);
-- bit
SELECT [system_type_name]
FROM sys.dm_exec_describe_first_result_set('SELECT 1 AS [BITorINT?]', NULL, NULL);
-- int
SELECT [system_type_name]
FROM sys.dm_exec_describe_first_result_set('SELECT CONVERT(BIT, 1) AS [BITorINT?]
UNION ALL
SELECT 1', NULL, NULL);
-- int
下次提示:
关于c# - 读取位时来自 SqlDataReader 的无效转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30495038/
我正在尝试将一个字符串逐个字符地复制到另一个字符串中。目的不是复制整个字符串,而是复制其中的一部分(我稍后会为此做一些条件......) 但我不知道如何使用迭代器。 你能帮帮我吗? std::stri
我想将 void 指针转换为结构引用。 结构的最小示例: #include "Interface.h" class Foo { public: Foo() : mAddress((uint
这有点烦人:我有一个 div,它从窗口的左上角开始过渡,即使它位于文档的其他任何位置。我试过 usign -webkit-transform-origin 但没有成功,也许我用错了。有人可以帮助我吗?
假设,如果将 CSS3 转换/转换/动画分配给 DOM 元素,我是否可以检测到该过程的状态? 我想这样做的原因是因为我正在寻找类似过渡链的东西,例如,在前一个过渡之后运行一个过渡。 最佳答案 我在 h
最近我遇到了“不稳定”屏幕,这很可能是由 CSS 转换引起的。事实上,它只发生在 Chrome 浏览器 上(可能还有 Safari,因为一些人也报告了它)。知道如何让它看起来光滑吗?此外,您可能会注意
我正在开发一个简单的 slider ,它使用 CSS 过渡来为幻灯片设置动画。我用一些基本样式和一些 javascript 创建了一支笔 here .注意:由于 Codepen 使用 Prefixfr
我正在使用以下代码返回 IList: public IList FindCodesByCountry(string country) { var query =
如何设计像这样的操作: 计算 转化 翻译 例如:从“EUR”转换为“CNY”金额“100”。 这是 /convert?from=EUR&to=CNY&amount=100 RESTful 吗? 最佳答
我使用 jquery 组合了一个图像滚动器,如下所示 function rotateImages(whichHolder, start) { var images = $('#' +which
如何使用 CSS (-moz-transform) 更改一个如下所示的 div: 最佳答案 你可以看看Mozilla Developer Center .甚至还有例子。 但是,在我看来,您的具体示例不
我需要帮助我正在尝试在选中和未选中的汉堡菜单上实现动画。我能够为菜单设置动画,但我不知道如何在转换为 0 时为左菜单动画设置动画 &__menu { transform: translateX(
我正在为字典格式之间的转换而苦苦挣扎:我正在尝试将下面的项目数组转换为下面的结果数组。本质上是通过在项目第一个元素中查找重复项,然后仅在第一个参数不同时才将文件添加到结果集中。 var items:[
如果我有两个定义相同的结构,那么在它们之间进行转换的最佳方式是什么? struct A { int i; float f; }; struct B { int i; float f; }; void
我编写了一个 javascript 代码,可以将视口(viewport)从一个链接滑动到另一个链接。基本上一切正常,你怎么能在那里看到http://jsfiddle.net/DruwJ/8/ 我现在的
我需要将文件上传到 meteor ,对其进行一些图像处理(必要时进行图像转换,从图像生成缩略图),然后将其存储在外部图像存储服务器(s3)中。这应该尽可能快。 您对 nodejs 图像处理库有什么建议
刚开始接触KDB+,有一些问题很难从Q for Mortals中得到。 说,这里 http://code.kx.com/wiki/JB:QforMortals2/casting_and_enumera
我在这里的一个项目中使用 JSF 1.2 和 IceFaces 1.8。 我有一个页面,它基本上是一大堆浮点数字段的大编辑网格。这是通过 inputText 实现的页面上的字段指向具有原始值的值对象
ScnMatrix4 是一个 4x4 矩阵。我的问题是什么矩阵行对应于位置(ScnVector3),旋转(ScnVector4),比例(ScnVector3)。第 4 行是空的吗? 编辑: 我玩弄了
恐怕我是 Scala 新手: 我正在尝试根据一些简单的逻辑将 Map 转换为新 Map: val postVals = Map("test" -> "testing1", "test2" -> "te
输入: This is sample 1 This is sample 2 输出: ~COLOR~[Green]This is sample 1~COLOR~[Red]This is sam
我是一名优秀的程序员,十分优秀!