本文介绍了如何转换日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我正在从数据库中重新执行以下查询。 选择 PFloanApplications。*,convert(varchar( 20 ),PFloanApplications.applicationdate, 111 ) as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber,HREmployeeInfo.FirstName + ' ' + HREmployeeInfo.Middlename + ' ' + HREmployeeInfo.lastname作为EmployeeName,PFLoanPurpose.LoanPurposeName 来自 PFLoanApplications左外加入 HFLmployeeInfo on PFLoanApplications.EmployeeId = HREmployeeInfo.EmployeeId left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId = PFLoanPurpose.LoanPurposeId where (PFLoanApplications.ApplicationDate介于' date.text'和' + dojto.Text +')和(PFLoanApplications.IsDeleted IS NULL)和(PFLoanApplications.ApprovedStatus null ) 问题是,当它在所选行之间找到它显示数据时,无论何时找不到该行,它都会显示以下错误。 消息 242 ,等级 16 ,状态 3 ,行 1 转换 char 数据类型 datetime 数据 type 超出范围 datetime 值。 请告诉我如何更正此错误。解决方案 System.Globalization.CultureInfo cul = new System.Globalization.CultureInfo( en-GB) ; DateTime current_date; DateTime current_date1; current_date = DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault); current_date1 = DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault); 其中 colum_name ' + current_date.ToShortDateString()+' 和 ' + current_date1.ToShortDateString()+' Quote: PFLoanApplications.ApplicationDate' date.text'和'+ dojto.Text +' 不要这样做,而是去参考下面的参数化查询... 选择PFloanApplications。*,convert(varchar( 20 ),PFloanApplications.applicationdate, 111 ) as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber,HREmployeeInfo.FirstName + ' ' + HREmployeeInfo.Middlename + ' ' + HREmployeeInfo.lastname作为EmployeeName,PFLoanPurpose.LoanPurposeName 来自 PFLoanApplications左外 join HREmployeeInfo on PFLoanApplications.EmployeeId = HREmployeeInfo.EmployeeId left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId = PFLoanPurpose.LoanPurposeId where ( PFLoanApplications.ApplicationDate BETWEEN @StartDate AND @EndDate )和(PFLoanApplications.IsDeleted IS NULL)和(PFLoanApplications.ApprovedStatus null ) DateTime startDate = DateTime.Parse(date.Text); DateTime endDate = DateTime.Parse(dojto.Text); yourCommand.Parameters.AddWithValue( @ StartDate,startDate); yourCommand.Parameters.AddWithValue( @ EndDate,endDate); Hello,I am reteriving the below query from database.select PFloanApplications.*,convert(varchar(20),PFloanApplications.applicationdate,111)as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber, HREmployeeInfo.FirstName+' '+HREmployeeInfo.Middlename+' '+HREmployeeInfo.lastname As EmployeeName,PFLoanPurpose.LoanPurposeName from PFLoanApplications left outer join HREmployeeInfo on PFLoanApplications.EmployeeId=HREmployeeInfo.EmployeeId left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId=PFLoanPurpose.LoanPurposeId where (PFLoanApplications.ApplicationDate between 'date.text' and '" + dojto.Text + "')and(PFLoanApplications.IsDeleted IS NULL)and(PFLoanApplications.ApprovedStatus is null)The problem is that when it finds the between the selected rows it shows the data and whenever it doesn't find the row it shows the following error.Msg 242, Level 16, State 3, Line 1The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.please tell how to correct this error. 解决方案 System.Globalization.CultureInfo cul = new System.Globalization.CultureInfo("en-GB");DateTime current_date;DateTime current_date1;current_date=DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault);current_date1=DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault);in you querywhere colum_name between '"+current_date.ToShortDateString()+"' and '"+current_date1.ToShortDateString()+"'Quote:PFLoanApplications.ApplicationDate between 'date.text' and '" + dojto.Text + "'Don't do like this, instead go for parameterized query something like below...Select PFloanApplications.*,convert(varchar(20),PFloanApplications.applicationdate,111)as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber, HREmployeeInfo.FirstName+' '+HREmployeeInfo.Middlename+' '+HREmployeeInfo.lastname As EmployeeName,PFLoanPurpose.LoanPurposeName from PFLoanApplications left outer join HREmployeeInfo on PFLoanApplications.EmployeeId=HREmployeeInfo.EmployeeId left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId=PFLoanPurpose.LoanPurposeId where (PFLoanApplications.ApplicationDate BETWEEN @StartDate AND @EndDate)and(PFLoanApplications.IsDeleted IS NULL)and(PFLoanApplications.ApprovedStatus is null)DateTime startDate = DateTime.Parse(date.Text);DateTime endDate = DateTime.Parse(dojto.Text);yourCommand.Parameters.AddWithValue("@StartDate", startDate);yourCommand.Parameters.AddWithValue("@EndDate", endDate); 这篇关于如何转换日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 05:17