将varchar数据类型转换为日期时间数据类型会导致超出范围的值C#

时间:2015-04-07 07:50:52

标签: asp.net

下面是我通过从今天的日期减去TimeSpan来计算日期的代码。

DateTime todayDate = DateTime.Today;
TimeSpan minTS = new TimeSpan(6570, 0, 0, 0, 0);
TimeSpan maxTS = new TimeSpan(7300, 0, 0, 0, 0);
DateTime minDate = todayDate.Subtract(minTS);
DateTime maxDate = todayDate.Subtract(maxTS);

我的查询如下:

sql = "select AId,AName,DOB,Religion from Applicant,Creator as C where (C.CId = Applicant.CId and Approved = 'Approved' and Gender !='" + gend + "' and AId !=" + id + ") and (Applicant.Status = '" + status.Items[status.SelectedIndex].ToString() + "' and Applicant.DOB BETWEEN '" + maxDate + "' and '" + minDate + "')";
                    cmd = new SqlCommand(sql, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);      // use your existing SqlCommand here (don't use select *)
                    DataSet ds = new DataSet();                       // create a DataSet object to hold you table(s)... this can contain more than 1 table
                    da.Fill(ds, "Comment");      

这对我来说很好。但是当我将TimeSpan的值更改为

TimeSpan(7300, 0, 0, 0, 0);
TimeSpan maxTS = new TimeSpan(9125, 0, 0, 0, 0);

我收到以下错误: 将varchar数据类型转换为日期时间数据类型会导致超出范围的值。 我希望查询没有问题,因为相同的查询适用于TimeSpan(6570,0,0,0,0)和TimeSpan(7300,0,0,0,0)。当我将其更改为TimeSpan(7300,0,0,0,0)和TimeSpan(9125,0,0,0,0)

时会出错

我没有得到我错的地方。

0 个答案:

没有答案