>我有一个Windows窗体应用程序,其中许多值已成功写入我的数据库。它使用DataSet
和所有常用的自动生成的表适配器等。我没有摆弄过这些。我只需将字段从"数据源"选项卡拖放到我的表单上即可。自动生成的 SQL 看起来正常。
我有两个以相同方式绑定DateTimePicker
控件数据,如果我使用默认值,它们都不会写出到数据库中。SQL Server 中的数据格式为 datetime。
从表单加载方法中,我像这样设置初始日期值。
// Set the Date entered to Today
dateTimePickerDateEntered.Value = DateTime.Now;
// Set the call back date to Today + 7 days
dateTimePickerCallBackDate.Value = DateTime.Now.AddDays(7);
如果我使用 datetimepicker 控件手动更改日期,则会成功保存到数据库中。我确实在谷歌上搜索了这个问题,并找到了建议我已经在做的事情的解决方案。
默认值在屏幕上看起来不错,如果我今天实际选择,似乎不会改变。
任何想法我应该做什么不同?
谢谢 大卫
更新
我使用默认日期执行了保存,并使用名为SQL Profile Express的工具从服务器捕获了生成的sql。
执行了三个命令。
执行sp_resetConnection
执行sp_executesql插入
执行sp_executesql更新
我不确定它为什么要进行更新。它似乎正在尝试更新现有的不相关记录,我正在插入一条新记录。 提交的主键 QuoteID 是一个标识字段。
这是插入
`exec sp_executesql N'INSERT INTO [Quote] ([DateEntered], [ContactName], [PreparedFor], [MailAddress1], [MailAddress2], [MASuburb], [MAState], [MAPcode], [SPNumber], [SPAddress1], [SPAddress2], [SPSuburb], [SPState], [SPPcode], [NumberOfUnits], [PricePerUnit], [TotalAnnualFee], [Stationary], [ContactPhoneW], [ContactPhoneH], [ContactPhoneM], [Fax], [Email], [WhyChooseUs], [CurrentlyManagedBy], [DateSent], [CallBackDate], [StatusId], [DocumentLink], [EnteredBy], [PictureLocation]) VALUES (@DateEntered, @ContactName, @PreparedFor, @MailAddress1, @MailAddress2, @MASuburb, @MAState, @MAPcode, @SPNumber, @SPAddress1, @SPAddress2, @SPSuburb, @SPState, @SPPcode, @NumberOfUnits, @PricePerUnit, @TotalAnnualFee, @Stationary, @ContactPhoneW, @ContactPhoneH, @ContactPhoneM, @Fax, @Email, @WhyChooseUs, @CurrentlyManagedBy, @DateSent, @CallBackDate, @StatusId, @DocumentLink, @EnteredBy, @PictureLocation);
SELECT QuoteId, DateEntered, ContactName, PreparedFor, MailAddress1, MailAddress2, MASuburb, MAState, MAPcode, SPNumber, SPAddress1, SPAddress2, SPSuburb, SPState, SPPcode, NumberOfUnits, PricePerUnit, TotalAnnualFee, Stationary, ContactPhoneW, ContactPhoneH, ContactPhoneM, Fax, Email, WhyChooseUs, CurrentlyManagedBy, DateSent, CallBackDate, StatusId, DocumentLink, EnteredBy, PictureLocation FROM Quote WHERE (QuoteId = SCOPE_IDENTITY())',N'@DateEntered datetime,@ContactName char(9),@PreparedFor char(9),@MailAddress1 char(16),@MailAddress2 char(16),@MASuburb char(19),@MAState char(3),@MAPcode char(4),@SPNumber char(6),@SPAddress1 char(18),@SPAddress2 char(18),@SPSuburb char(13),@SPState char(2),@SPPcode char(4),@NumberOfUnits int,@PricePerUnit money,@TotalAnnualFee money,@Stationary money,@ContactPhoneW char(12),@ContactPhoneH char(12),@ContactPhoneM char(12),@Fax char(12),@Email char(19),@WhyChooseUs char(13),@CurrentlyManagedBy char(20),@DateSent datetime,@CallBackDate datetime,@StatusId int,@DocumentLink char(8000),@EnteredBy char(8000),@PictureLocation varchar(63)',@DateEntered=NULL,@ContactName='Joe Smith',@PreparedFor='Bob Jones',@MailAddress1='MAIL ADDRESS ONE',@MailAddress2='MAIL ADDRESS TWO',@MASuburb='MAIL ADDRESS SUBURB',@MAState='NSW',@MAPcode='2000',@SPNumber='123456',@SPAddress1='STRATA ADDRESS ONE',@SPAddress2='STRATA ADDRESS TWO',@SPSuburb='STRATA SUBURB',@SPState='SA',@SPPcode='2001',@NumberOfUnits=100,@PricePerUnit=200.0000,@TotalAnnualFee=20000.0000,@Stationary=3000.0000,@ContactPhoneW='01 123456789',@ContactPhoneH='02 123456789',@ContactPhoneM='03 123456789',@Fax='04 123456789',@Email='my@emailaddress.com',@WhyChooseUs='WHY CHOOSE US',@CurrentlyManagedBy='CURRENTLY MANAGED BY',@DateSent=NULL,@CallBackDate=NULL,@StatusId=NULL,@DocumentLink=NULL,@EnteredBy=NULL,@PictureLocation='C:TempQuoteMasterPlusPhotosSTRATAADDRESSONESTRATASUBURB.jpg'
go`
这是更新
exec sp_executesql N'UPDATE [Quote] SET [DateEntered] = @DateEntered, [ContactName] = @ContactName, [PreparedFor] = @PreparedFor, [MailAddress1] = @MailAddress1, [MailAddress2] = @MailAddress2, [MASuburb] = @MASuburb, [MAState] = @MAState, [MAPcode] = @MAPcode, [SPNumber] = @SPNumber, [SPAddress1] = @SPAddress1, [SPAddress2] = @SPAddress2, [SPSuburb] = @SPSuburb, [SPState] = @SPState, [SPPcode] = @SPPcode, [NumberOfUnits] = @NumberOfUnits, [PricePerUnit] = @PricePerUnit, [TotalAnnualFee] = @TotalAnnualFee, [Stationary] = @Stationary, [ContactPhoneW] = @ContactPhoneW, [ContactPhoneH] = @ContactPhoneH, [ContactPhoneM] = @ContactPhoneM, [Fax] = @Fax, [Email] = @Email, [WhyChooseUs] = @WhyChooseUs, [CurrentlyManagedBy] = @CurrentlyManagedBy, [DateSent] = @DateSent, [CallBackDate] = @CallBackDate, [StatusId] = @StatusId, [DocumentLink] = @DocumentLink, [EnteredBy] = @EnteredBy, [PictureLocation] = @PictureLocation WHERE (([QuoteId] = @Original_QuoteId) AND ((@IsNull_DateEntered = 1 AND [DateEntered] IS NULL) OR ([DateEntered] = @Original_DateEntered)) AND ((@IsNull_ContactName = 1 AND [ContactName] IS NULL) OR ([ContactName] = @Original_ContactName)) AND ((@IsNull_PreparedFor = 1 AND [PreparedFor] IS NULL) OR ([PreparedFor] = @Original_PreparedFor)) AND ((@IsNull_MailAddress1 = 1 AND [MailAddress1] IS NULL) OR ([MailAddress1] = @Original_MailAddress1)) AND ((@IsNull_MailAddress2 = 1 AND [MailAddress2] IS NULL) OR ([MailAddress2] = @Original_MailAddress2)) AND ((@IsNull_MASuburb = 1 AND [MASuburb] IS NULL) OR ([MASuburb] = @Original_MASuburb)) AND ((@IsNull_MAState = 1 AND [MAState] IS NULL) OR ([MAState] = @Original_MAState)) AND ((@IsNull_MAPcode = 1 AND [MAPcode] IS NULL) OR ([MAPcode] = @Original_MAPcode)) AND ((@IsNull_SPNumber = 1 AND [SPNumber] IS NULL) OR ([SPNumber] = @Original_SPNumber)) AND ((@IsNull_SPAddress1 = 1 AND [SPAddress1] IS NULL) OR ([SPAddress1] = @Original_SPAddress1)) AND ((@IsNull_SPAddress2 = 1 AND [SPAddress2] IS NULL) OR ([SPAddress2] = @Original_SPAddress2)) AND ((@IsNull_SPSuburb = 1 AND [SPSuburb] IS NULL) OR ([SPSuburb] = @Original_SPSuburb)) AND ((@IsNull_SPState = 1 AND [SPState] IS NULL) OR ([SPState] = @Original_SPState)) AND ((@IsNull_SPPcode = 1 AND [SPPcode] IS NULL) OR ([SPPcode] = @Original_SPPcode)) AND ((@IsNull_NumberOfUnits = 1 AND [NumberOfUnits] IS NULL) OR ([NumberOfUnits] = @Original_NumberOfUnits)) AND ((@IsNull_PricePerUnit = 1 AND [PricePerUnit] IS NULL) OR ([PricePerUnit] = @Original_PricePerUnit)) AND ((@IsNull_TotalAnnualFee = 1 AND [TotalAnnualFee] IS NULL) OR ([TotalAnnualFee] = @Original_TotalAnnualFee)) AND ((@IsNull_Stationary = 1 AND [Stationary] IS NULL) OR ([Stationary] = @Original_Stationary)) AND ((@IsNull_ContactPhoneW = 1 AND [ContactPhoneW] IS NULL) OR ([ContactPhoneW] = @Original_ContactPhoneW)) AND ((@IsNull_ContactPhoneH = 1 AND [ContactPhoneH] IS NULL) OR ([ContactPhoneH] = @Original_ContactPhoneH)) AND ((@IsNull_ContactPhoneM = 1 AND [ContactPhoneM] IS NULL) OR ([ContactPhoneM] = @Original_ContactPhoneM)) AND ((@IsNull_Fax = 1 AND [Fax] IS NULL) OR ([Fax] = @Original_Fax)) AND ((@IsNull_Email = 1 AND [Email] IS NULL) OR ([Email] = @Original_Email)) AND ((@IsNull_WhyChooseUs = 1 AND [WhyChooseUs] IS NULL) OR ([WhyChooseUs] = @Original_WhyChooseUs)) AND ((@IsNull_CurrentlyManagedBy = 1 AND [CurrentlyManagedBy] IS NULL) OR ([CurrentlyManagedBy] = @Original_CurrentlyManagedBy)) AND ((@IsNull_DateSent = 1 AND [DateSent] IS NULL) OR ([DateSent] = @Original_DateSent)) AND ((@IsNull_CallBackDate = 1 AND [CallBackDate] IS NULL) OR ([CallBackDate] = @Original_CallBackDate)) AND ((@IsNull_StatusId = 1 AND [StatusId] IS NULL) OR ([StatusId] = @Original_StatusId)) AND ((@IsNull_DocumentLink = 1 AND [DocumentLink] IS NULL) OR ([DocumentLink] = @Original_DocumentLink)) AND ((@IsNull_EnteredBy = 1 AND [EnteredBy] IS NULL) OR ([EnteredBy] = @Original_EnteredBy)) AND ((@IsNull_PictureLocation = 1 AND [PictureLocation] IS NULL) OR ([PictureLocation] = @Original_PictureLocation)));
SELECT QuoteId, DateEntered, ContactName, PreparedFor, MailAddress1, MailAddress2, MASuburb, MAState, MAPcode, SPNumber, SPAddress1, SPAddress2, SPSuburb, SPState, SPPcode, NumberOfUnits, PricePerUnit, TotalAnnualFee, Stationary, ContactPhoneW, ContactPhoneH, ContactPhoneM, Fax, Email, WhyChooseUs, CurrentlyManagedBy, DateSent, CallBackDate, StatusId, DocumentLink, EnteredBy, PictureLocation FROM Quote WHERE (QuoteId = @QuoteId)',N'@DateEntered datetime,@ContactName char(50),@PreparedFor char(50),@MailAddress1 char(50),@MailAddress2 char(8000),@MASuburb char(50),@MAState char(3),@MAPcode char(10),@SPNumber char(10),@SPAddress1 char(50),@SPAddress2 char(8000),@SPSuburb char(50),@SPState char(3),@SPPcode char(10),@NumberOfUnits int,@PricePerUnit money,@TotalAnnualFee money,@Stationary money,@ContactPhoneW char(8000),@ContactPhoneH char(8000),@ContactPhoneM char(8000),@Fax char(8000),@Email char(50),@WhyChooseUs char(50),@CurrentlyManagedBy char(50),@DateSent datetime,@CallBackDate datetime,@StatusId int,@DocumentLink char(8000),@EnteredBy char(15),@PictureLocation varchar(8000),@Original_QuoteId int,@IsNull_DateEntered int,@Original_DateEntered datetime,@IsNull_ContactName int,@Original_ContactName char(50),@IsNull_PreparedFor int,@Original_PreparedFor char(50),@IsNull_MailAddress1 int,@Original_MailAddress1 char(50),@IsNull_MailAddress2 int,@Original_MailAddress2 char(8000),@IsNull_MASuburb int,@Original_MASuburb char(50),@IsNull_MAState int,@Original_MAState char(3),@IsNull_MAPcode int,@Original_MAPcode char(10),@IsNull_SPNumber int,@Original_SPNumber char(10),@IsNull_SPAddress1 int,@Original_SPAddress1 char(50),@IsNull_SPAddress2 int,@Original_SPAddress2 char(8000),@IsNull_SPSuburb int,@Original_SPSuburb char(50),@IsNull_SPState int,@Original_SPState char(3),@IsNull_SPPcode int,@Original_SPPcode char(10),@IsNull_NumberOfUnits int,@Original_NumberOfUnits int,@IsNull_PricePerUnit int,@Original_PricePerUnit money,@IsNull_TotalAnnualFee int,@Original_TotalAnnualFee money,@IsNull_Stationary int,@Original_Stationary money,@IsNull_ContactPhoneW int,@Original_ContactPhoneW char(8000),@IsNull_ContactPhoneH int,@Original_ContactPhoneH char(8000),@IsNull_ContactPhoneM int,@Original_ContactPhoneM char(8000),@IsNull_Fax int,@Original_Fax char(8000),@IsNull_Email int,@Original_Email char(50),@IsNull_WhyChooseUs int,@Original_WhyChooseUs char(50),@IsNull_CurrentlyManagedBy int,@Original_CurrentlyManagedBy char(50),@IsNull_DateSent int,@Original_DateSent datetime,@IsNull_CallBackDate int,@Original_CallBackDate datetime,@IsNull_StatusId int,@Original_StatusId int,@IsNull_DocumentLink int,@Original_DocumentLink char(8000),@IsNull_EnteredBy int,@Original_EnteredBy char(15),@IsNull_PictureLocation int,@Original_PictureLocation varchar(8000),@QuoteId int',@DateEntered='2017-05-18 10:36:54.657',@ContactName='Herve Commiegnes ',@PreparedFor='Herve Commiegnes ',@MailAddress1='21 Westbrook Street ',@MailAddress2=NULL,@MASuburb='Beverly Hills ',@MAState='NSW',@MAPcode='2209 ',@SPNumber='54038 ',@SPAddress1='29 Riverside Cres ',@SPAddress2=NULL,@SPSuburb='Marrickville ',@SPState='NSW',@SPPcode='2204 ',@NumberOfUnits=4,@PricePerUnit=132.0000,@TotalAnnualFee=NULL,@Stationary=200.0000,@ContactPhoneW=NULL,@ContactPhoneH=NULL,@ContactPhoneM=NULL,@Fax=NULL,@Email='hervec@optushome.com.au ',@WhyChooseUs='Recommended ',@CurrentlyManagedBy='Fabos ',@DateSent='2005-12-28 00:00:00',@CallBackDate='2017-05-25 10:36:54.657',@StatusId=2,@DocumentLink=NULL,@EnteredBy='mpollard ',@PictureLocation=NULL,@Original_QuoteId=19,@IsNull_DateEntered=0,@Original_DateEntered='2017-05-17 09:33:37.480',@IsNull_ContactName=0,@Original_ContactName='Herve Commiegnes ',@IsNull_PreparedFor=0,@Original_PreparedFor='Herve Commiegnes ',@IsNull_MailAddress1=0,@Original_MailAddress1='21 Westbrook Street ',@IsNull_MailAddress2=1,@Original_MailAddress2=NULL,@IsNull_MASuburb=0,@Original_MASuburb='Beverly Hills ',@IsNull_MAState=0,@Original_MAState='NSW',@IsNull_MAPcode=0,@Original_MAPcode='2209 ',@IsNull_SPNumber=0,@Original_SPNumber='54038 ',@IsNull_SPAddress1=0,@Original_SPAddress1='29 Riverside Cres ',@IsNull_SPAddress2=1,@Original_SPAddress2=NULL,@IsNull_SPSuburb=0,@Original_SPSuburb='Marrickville ',@IsNull_SPState=0,@Original_SPState='NSW',@IsNull_SPPcode=0,@Original_SPPcode='2204 ',@IsNull_NumberOfUnits=0,@Original_NumberOfUnits=4,@IsNull_PricePerUnit=0,@Original_PricePerUnit=132.0000,@IsNull_TotalAnnualFee=1,@Original_TotalAnnualFee=NULL,@IsNull_Stationary=0,@Original_Stationary=200.0000,@IsNull_ContactPhoneW=1,@Original_ContactPhoneW=NULL,@IsNull_ContactPhoneH=1,@Original_ContactPhoneH=NULL,@IsNull_ContactPhoneM=1,@Original_ContactPhoneM=NULL,@IsNull_Fax=1,@Original_Fax=NULL,@IsNull_Email=0,@Original_Email='hervec@optushome.com.au ',@IsNull_WhyChooseUs=0,@Original_WhyChooseUs='Recommended ',@IsNull_CurrentlyManagedBy=0,@Original_CurrentlyManagedBy='Fabos ',@IsNull_DateSent=0,@Original_DateSent='2005-12-28 00:00:00',@IsNull_CallBackDate=0,@Original_CallBackDate='2017-05-24 09:33:37.480',@IsNull_StatusId=0,@Original_StatusId=2,@IsNull_DocumentLink=1,@Original_DocumentLink=NULL,@IsNull_EnteredBy=0,@Original_EnteredBy='mpollard ',@IsNull_PictureLocation=1,@Original_PictureLocation=NULL,@QuoteId=19
go
然后我再次做了几乎相同的操作,但这次在日期时间选择器中设置日期,而不是将它们保留在我的默认日期。 我选择了明天(2017 年 5 月 19 日)作为输入日期,选择了 2017 年 5 月 26 日作为回拨日期。 这一次,除"输入日期"之外的所有字段都插入到数据库中。
只是为了清楚。 这次 CallBackDate 有效,而 Date 输入没有。
生成的三个命令相同,重置,插入和更新
谁能发现这里发生了什么?
这是插入命令
exec sp_executesql N'INSERT INTO [Quote] ([DateEntered], [ContactName], [PreparedFor], [MailAddress1], [MailAddress2], [MASuburb], [MAState], [MAPcode], [SPNumber], [SPAddress1], [SPAddress2], [SPSuburb], [SPState], [SPPcode], [NumberOfUnits], [PricePerUnit], [TotalAnnualFee], [Stationary], [ContactPhoneW], [ContactPhoneH], [ContactPhoneM], [Fax], [Email], [WhyChooseUs], [CurrentlyManagedBy], [DateSent], [CallBackDate], [StatusId], [DocumentLink], [EnteredBy], [PictureLocation]) VALUES (@DateEntered, @ContactName, @PreparedFor, @MailAddress1, @MailAddress2, @MASuburb, @MAState, @MAPcode, @SPNumber, @SPAddress1, @SPAddress2, @SPSuburb, @SPState, @SPPcode, @NumberOfUnits, @PricePerUnit, @TotalAnnualFee, @Stationary, @ContactPhoneW, @ContactPhoneH, @ContactPhoneM, @Fax, @Email, @WhyChooseUs, @CurrentlyManagedBy, @DateSent, @CallBackDate, @StatusId, @DocumentLink, @EnteredBy, @PictureLocation);
SELECT QuoteId, DateEntered, ContactName, PreparedFor, MailAddress1, MailAddress2, MASuburb, MAState, MAPcode, SPNumber, SPAddress1, SPAddress2, SPSuburb, SPState, SPPcode, NumberOfUnits, PricePerUnit, TotalAnnualFee, Stationary, ContactPhoneW, ContactPhoneH, ContactPhoneM, Fax, Email, WhyChooseUs, CurrentlyManagedBy, DateSent, CallBackDate, StatusId, DocumentLink, EnteredBy, PictureLocation FROM Quote WHERE (QuoteId = SCOPE_IDENTITY())',N'@DateEntered datetime,@ContactName char(12),@PreparedFor char(12),@MailAddress1 char(16),@MailAddress2 char(16),@MASuburb char(11),@MAState char(3),@MAPcode char(4),@SPNumber char(8),@SPAddress1 char(18),@SPAddress2 char(18),@SPSuburb char(6),@SPState char(2),@SPPcode char(4),@NumberOfUnits int,@PricePerUnit money,@TotalAnnualFee money,@Stationary money,@ContactPhoneW char(11),@ContactPhoneH char(11),@ContactPhoneM char(11),@Fax char(11),@Email char(12),@WhyChooseUs char(13),@CurrentlyManagedBy char(20),@DateSent datetime,@CallBackDate datetime,@StatusId int,@DocumentLink char(8000),@EnteredBy char(8000),@PictureLocation varchar(57)',@DateEntered='2017-05-19 11:38:53',@ContactName='CONTACT NAME',@PreparedFor='PREPARED FOR',@MailAddress1='MAIL ADDRESS ONE',@MailAddress2='MAIL ADDRESS TWO',@MASuburb='MAIL SUBURB',@MAState='NSW',@MAPcode='2000',@SPNumber='12345678',@SPAddress1='STRATA ADDRESS ONE',@SPAddress2='STRATA ADDRESS TWO',@SPSuburb='SUBURB',@SPState='SA',@SPPcode='2001',@NumberOfUnits=100,@PricePerUnit=200.0000,@TotalAnnualFee=20000.0000,@Stationary=100.0000,@ContactPhoneW='01 12345678',@ContactPhoneH='02 12345678',@ContactPhoneM='03 12345678',@Fax='03 12345678',@Email='my@email.com',@WhyChooseUs='WHY CHOOSE US',@CurrentlyManagedBy='CURRENTLY MANAGED BY',@DateSent=NULL,@CallBackDate='2017-05-26 11:38:53',@StatusId=NULL,@DocumentLink=NULL,@EnteredBy=NULL,@PictureLocation='C:TempQuoteMasterPlusPhotosSTRATAADDRESSONESUBURB.jpg'
go
这是更新命令
exec sp_executesql N'UPDATE [Quote] SET [DateEntered] = @DateEntered, [ContactName] = @ContactName, [PreparedFor] = @PreparedFor, [MailAddress1] = @MailAddress1, [MailAddress2] = @MailAddress2, [MASuburb] = @MASuburb, [MAState] = @MAState, [MAPcode] = @MAPcode, [SPNumber] = @SPNumber, [SPAddress1] = @SPAddress1, [SPAddress2] = @SPAddress2, [SPSuburb] = @SPSuburb, [SPState] = @SPState, [SPPcode] = @SPPcode, [NumberOfUnits] = @NumberOfUnits, [PricePerUnit] = @PricePerUnit, [TotalAnnualFee] = @TotalAnnualFee, [Stationary] = @Stationary, [ContactPhoneW] = @ContactPhoneW, [ContactPhoneH] = @ContactPhoneH, [ContactPhoneM] = @ContactPhoneM, [Fax] = @Fax, [Email] = @Email, [WhyChooseUs] = @WhyChooseUs, [CurrentlyManagedBy] = @CurrentlyManagedBy, [DateSent] = @DateSent, [CallBackDate] = @CallBackDate, [StatusId] = @StatusId, [DocumentLink] = @DocumentLink, [EnteredBy] = @EnteredBy, [PictureLocation] = @PictureLocation WHERE (([QuoteId] = @Original_QuoteId) AND ((@IsNull_DateEntered = 1 AND [DateEntered] IS NULL) OR ([DateEntered] = @Original_DateEntered)) AND ((@IsNull_ContactName = 1 AND [ContactName] IS NULL) OR ([ContactName] = @Original_ContactName)) AND ((@IsNull_PreparedFor = 1 AND [PreparedFor] IS NULL) OR ([PreparedFor] = @Original_PreparedFor)) AND ((@IsNull_MailAddress1 = 1 AND [MailAddress1] IS NULL) OR ([MailAddress1] = @Original_MailAddress1)) AND ((@IsNull_MailAddress2 = 1 AND [MailAddress2] IS NULL) OR ([MailAddress2] = @Original_MailAddress2)) AND ((@IsNull_MASuburb = 1 AND [MASuburb] IS NULL) OR ([MASuburb] = @Original_MASuburb)) AND ((@IsNull_MAState = 1 AND [MAState] IS NULL) OR ([MAState] = @Original_MAState)) AND ((@IsNull_MAPcode = 1 AND [MAPcode] IS NULL) OR ([MAPcode] = @Original_MAPcode)) AND ((@IsNull_SPNumber = 1 AND [SPNumber] IS NULL) OR ([SPNumber] = @Original_SPNumber)) AND ((@IsNull_SPAddress1 = 1 AND [SPAddress1] IS NULL) OR ([SPAddress1] = @Original_SPAddress1)) AND ((@IsNull_SPAddress2 = 1 AND [SPAddress2] IS NULL) OR ([SPAddress2] = @Original_SPAddress2)) AND ((@IsNull_SPSuburb = 1 AND [SPSuburb] IS NULL) OR ([SPSuburb] = @Original_SPSuburb)) AND ((@IsNull_SPState = 1 AND [SPState] IS NULL) OR ([SPState] = @Original_SPState)) AND ((@IsNull_SPPcode = 1 AND [SPPcode] IS NULL) OR ([SPPcode] = @Original_SPPcode)) AND ((@IsNull_NumberOfUnits = 1 AND [NumberOfUnits] IS NULL) OR ([NumberOfUnits] = @Original_NumberOfUnits)) AND ((@IsNull_PricePerUnit = 1 AND [PricePerUnit] IS NULL) OR ([PricePerUnit] = @Original_PricePerUnit)) AND ((@IsNull_TotalAnnualFee = 1 AND [TotalAnnualFee] IS NULL) OR ([TotalAnnualFee] = @Original_TotalAnnualFee)) AND ((@IsNull_Stationary = 1 AND [Stationary] IS NULL) OR ([Stationary] = @Original_Stationary)) AND ((@IsNull_ContactPhoneW = 1 AND [ContactPhoneW] IS NULL) OR ([ContactPhoneW] = @Original_ContactPhoneW)) AND ((@IsNull_ContactPhoneH = 1 AND [ContactPhoneH] IS NULL) OR ([ContactPhoneH] = @Original_ContactPhoneH)) AND ((@IsNull_ContactPhoneM = 1 AND [ContactPhoneM] IS NULL) OR ([ContactPhoneM] = @Original_ContactPhoneM)) AND ((@IsNull_Fax = 1 AND [Fax] IS NULL) OR ([Fax] = @Original_Fax)) AND ((@IsNull_Email = 1 AND [Email] IS NULL) OR ([Email] = @Original_Email)) AND ((@IsNull_WhyChooseUs = 1 AND [WhyChooseUs] IS NULL) OR ([WhyChooseUs] = @Original_WhyChooseUs)) AND ((@IsNull_CurrentlyManagedBy = 1 AND [CurrentlyManagedBy] IS NULL) OR ([CurrentlyManagedBy] = @Original_CurrentlyManagedBy)) AND ((@IsNull_DateSent = 1 AND [DateSent] IS NULL) OR ([DateSent] = @Original_DateSent)) AND ((@IsNull_CallBackDate = 1 AND [CallBackDate] IS NULL) OR ([CallBackDate] = @Original_CallBackDate)) AND ((@IsNull_StatusId = 1 AND [StatusId] IS NULL) OR ([StatusId] = @Original_StatusId)) AND ((@IsNull_DocumentLink = 1 AND [DocumentLink] IS NULL) OR ([DocumentLink] = @Original_DocumentLink)) AND ((@IsNull_EnteredBy = 1 AND [EnteredBy] IS NULL) OR ([EnteredBy] = @Original_EnteredBy)) AND ((@IsNull_PictureLocation = 1 AND [PictureLocation] IS NULL) OR ([PictureLocation] = @Original_PictureLocation)));
SELECT QuoteId, DateEntered, ContactName, PreparedFor, MailAddress1, MailAddress2, MASuburb, MAState, MAPcode, SPNumber, SPAddress1, SPAddress2, SPSuburb, SPState, SPPcode, NumberOfUnits, PricePerUnit, TotalAnnualFee, Stationary, ContactPhoneW, ContactPhoneH, ContactPhoneM, Fax, Email, WhyChooseUs, CurrentlyManagedBy, DateSent, CallBackDate, StatusId, DocumentLink, EnteredBy, PictureLocation FROM Quote WHERE (QuoteId = @QuoteId)',N'@DateEntered datetime,@ContactName char(50),@PreparedFor char(50),@MailAddress1 char(50),@MailAddress2 char(8000),@MASuburb char(50),@MAState char(3),@MAPcode char(10),@SPNumber char(10),@SPAddress1 char(50),@SPAddress2 char(8000),@SPSuburb char(50),@SPState char(3),@SPPcode char(10),@NumberOfUnits int,@PricePerUnit money,@TotalAnnualFee money,@Stationary money,@ContactPhoneW char(8000),@ContactPhoneH char(8000),@ContactPhoneM char(8000),@Fax char(8000),@Email char(50),@WhyChooseUs char(50),@CurrentlyManagedBy char(50),@DateSent datetime,@CallBackDate datetime,@StatusId int,@DocumentLink char(8000),@EnteredBy char(15),@PictureLocation varchar(8000),@Original_QuoteId int,@IsNull_DateEntered int,@Original_DateEntered datetime,@IsNull_ContactName int,@Original_ContactName char(50),@IsNull_PreparedFor int,@Original_PreparedFor char(50),@IsNull_MailAddress1 int,@Original_MailAddress1 char(50),@IsNull_MailAddress2 int,@Original_MailAddress2 char(8000),@IsNull_MASuburb int,@Original_MASuburb char(50),@IsNull_MAState int,@Original_MAState char(3),@IsNull_MAPcode int,@Original_MAPcode char(10),@IsNull_SPNumber int,@Original_SPNumber char(10),@IsNull_SPAddress1 int,@Original_SPAddress1 char(50),@IsNull_SPAddress2 int,@Original_SPAddress2 char(8000),@IsNull_SPSuburb int,@Original_SPSuburb char(50),@IsNull_SPState int,@Original_SPState char(3),@IsNull_SPPcode int,@Original_SPPcode char(10),@IsNull_NumberOfUnits int,@Original_NumberOfUnits int,@IsNull_PricePerUnit int,@Original_PricePerUnit money,@IsNull_TotalAnnualFee int,@Original_TotalAnnualFee money,@IsNull_Stationary int,@Original_Stationary money,@IsNull_ContactPhoneW int,@Original_ContactPhoneW char(8000),@IsNull_ContactPhoneH int,@Original_ContactPhoneH char(8000),@IsNull_ContactPhoneM int,@Original_ContactPhoneM char(8000),@IsNull_Fax int,@Original_Fax char(8000),@IsNull_Email int,@Original_Email char(50),@IsNull_WhyChooseUs int,@Original_WhyChooseUs char(50),@IsNull_CurrentlyManagedBy int,@Original_CurrentlyManagedBy char(50),@IsNull_DateSent int,@Original_DateSent datetime,@IsNull_CallBackDate int,@Original_CallBackDate datetime,@IsNull_StatusId int,@Original_StatusId int,@IsNull_DocumentLink int,@Original_DocumentLink char(8000),@IsNull_EnteredBy int,@Original_EnteredBy char(15),@IsNull_PictureLocation int,@Original_PictureLocation varchar(8000),@QuoteId int',@DateEntered='2017-05-18 11:38:53.353',@ContactName='Herve Commiegnes ',@PreparedFor='Herve Commiegnes ',@MailAddress1='21 Westbrook Street ',@MailAddress2=NULL,@MASuburb='Beverly Hills ',@MAState='NSW',@MAPcode='2209 ',@SPNumber='54038 ',@SPAddress1='29 Riverside Cres ',@SPAddress2=NULL,@SPSuburb='Marrickville ',@SPState='NSW',@SPPcode='2204 ',@NumberOfUnits=4,@PricePerUnit=132.0000,@TotalAnnualFee=NULL,@Stationary=200.0000,@ContactPhoneW=NULL,@ContactPhoneH=NULL,@ContactPhoneM=NULL,@Fax=NULL,@Email='hervec@optushome.com.au ',@WhyChooseUs='Recommended ',@CurrentlyManagedBy='Fabos ',@DateSent='2005-12-28 00:00:00',@CallBackDate='2017-05-25 11:38:53.353',@StatusId=2,@DocumentLink=NULL,@EnteredBy='mpollard ',@PictureLocation=NULL,@Original_QuoteId=19,@IsNull_DateEntered=0,@Original_DateEntered='2017-05-18 10:36:54.657',@IsNull_ContactName=0,@Original_ContactName='Herve Commiegnes ',@IsNull_PreparedFor=0,@Original_PreparedFor='Herve Commiegnes ',@IsNull_MailAddress1=0,@Original_MailAddress1='21 Westbrook Street ',@IsNull_MailAddress2=1,@Original_MailAddress2=NULL,@IsNull_MASuburb=0,@Original_MASuburb='Beverly Hills ',@IsNull_MAState=0,@Original_MAState='NSW',@IsNull_MAPcode=0,@Original_MAPcode='2209 ',@IsNull_SPNumber=0,@Original_SPNumber='54038 ',@IsNull_SPAddress1=0,@Original_SPAddress1='29 Riverside Cres ',@IsNull_SPAddress2=1,@Original_SPAddress2=NULL,@IsNull_SPSuburb=0,@Original_SPSuburb='Marrickville ',@IsNull_SPState=0,@Original_SPState='NSW',@IsNull_SPPcode=0,@Original_SPPcode='2204 ',@IsNull_NumberOfUnits=0,@Original_NumberOfUnits=4,@IsNull_PricePerUnit=0,@Original_PricePerUnit=132.0000,@IsNull_TotalAnnualFee=1,@Original_TotalAnnualFee=NULL,@IsNull_Stationary=0,@Original_Stationary=200.0000,@IsNull_ContactPhoneW=1,@Original_ContactPhoneW=NULL,@IsNull_ContactPhoneH=1,@Original_ContactPhoneH=NULL,@IsNull_ContactPhoneM=1,@Original_ContactPhoneM=NULL,@IsNull_Fax=1,@Original_Fax=NULL,@IsNull_Email=0,@Original_Email='hervec@optushome.com.au ',@IsNull_WhyChooseUs=0,@Original_WhyChooseUs='Recommended ',@IsNull_CurrentlyManagedBy=0,@Original_CurrentlyManagedBy='Fabos ',@IsNull_DateSent=0,@Original_DateSent='2005-12-28 00:00:00',@IsNull_CallBackDate=0,@Original_CallBackDate='2017-05-25 10:36:54.657',@IsNull_StatusId=0,@Original_StatusId=2,@IsNull_DocumentLink=1,@Original_DocumentLink=NULL,@IsNull_EnteredBy=0,@Original_EnteredBy='mpollard ',@IsNull_PictureLocation=1,@Original_PictureLocation=NULL,@QuoteId=19
go
更新 2
查看捕获的SQL的更新部分,这对我来说完全没有意义。 参数中的详细信息用于完全不同的记录。 我正在创建一个新记录,其中 ID 为 2122 或大约,更新的记录是 ID 19。 果然,如果我查看 ID 为 19 的记录,我会看到非常旧的数据~2006 年已更新为我的 2017 年日期。 Visual Studio到底是怎么想出这个SQL的?
更奇怪的是,它看起来总是更新记录 ID 19,这是表中的第一条记录。
据我所知:
- 插入 1 @DateEntered = 空
- 更新 1 @DateEntered = 2017-05-18 10:36:54.657
- 插入 2 @DateEntered = 2017-05-19 11:38:53
- update 2 @DateEntered = 2017-05-18 11:38:53.353。
没有看到代码,我无法确定,但我假设您在哪里设置@DateEntered参数不正确。
我不敢相信这竟然如此简单。
我的FormMainLoad看起来像这样。
1) 填充我的数据集(第一条记录作为当前记录加载)
2)设置我的默认日期(猜猜它改变了第一条记录)
3) 将对话框设置为开始新记录。
宾果游戏:它们的顺序错误
当我去保存时,它会对第一条记录进行更新,并在新记录上插入。
private void FormMain_Load(object sender, EventArgs e)
{
// This line of code loads data into the 'quoteDataSet.Quote' table. You can move, or remove it, as needed.
this.quoteTableAdapter.Fill(this.quoteDataSet.Quote);
// Set the Date entered to Today
dateTimePickerDateEntered.Value = DateTime.Now;
// Set the call back date to Today + 7 days
dateTimePickerCallBackDate.Value = DateTime.Now.AddDays(7);
//Start the dialog in Add New Record Mode
this.quoteBindingNavigator.Items["bindingNavigatorAddNewItem"].PerformClick();
}
所以我将命令的顺序更改为此
1) 加载数据集
2) 设置对话框以开始新记录
3) 设置默认日期
它有效。 有趣的是,我如何假设日期时间选择器的问题开始了我的野鹅追逐。 再加上我最初没有注意到第一条记录正在更新。
private void FormMain_Load(object sender, EventArgs e)
{
// This line of code loads data into the 'quoteDataSet.Quote' table. You can move, or remove it, as needed.
this.quoteTableAdapter.Fill(this.quoteDataSet.Quote);
//Start the dialog in Add New Record Mode
this.quoteBindingNavigator.Items["bindingNavigatorAddNewItem"].PerformClick();
// Set the Date entered to Today
dateTimePickerDateEntered.Value = DateTime.Now;
// Set the call back date to Today + 7 days
dateTimePickerCallBackDate.Value = DateTime.Now.AddDays(7);
}
感谢所有评论的人。 回答你的问题迫使我更仔细地看我的代码,并更慢、更深入地完成它,直到答案打到我的脸上。