错误 "Conversion failed when converting date and/or time from character string"
Error "Conversion failed when converting date and/or time from character string"
我有休假的 SQL 查询,在获取日期值时失败:
String strQuery = @"SELECT TOP 100000" +
"[dbo].[Service].[company_name] AS 'Company Name', [dbo].[Service].[status_description] AS 'Status Description'," +
"DATEPART(month, [dbo].[Service].[date_entered]) AS 'Month', COUNT([dbo].[Service].[status_description]) AS 'Count'" +
"FROM [dbo].[Service] WITH(NOLOCK)" +
"WHERE ([dbo].[Service].[status_description] = @Status)" +
// **** I used the following to get values from Ajax CalendarExtender
// **** (Ajax Format from: yyyy-MM-ddTHH:mm:ss.mmm to: yyyy-MM-ddT23:59:59.000) *******
"AND ([dbo].[Service].[date_entered] BETWEEN '@DateFrom' AND '@DateTo')" +
"GROUP BY [dbo].[Service].[company_name], [dbo].[Service].[status_description]," +
"DATEPART(month, [dbo].[Service].[date_entered])" +
"ORDER BY [dbo].[Service].[status_description] ASC;";
System.Globalization.CultureInfo en = new System.Globalization.CultureInfo("en-US");
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(strQuery + "", con);
cmd.Parameters.AddWithValue("@Status", DdlTickets.SelectedItem.Value);
cmd.Parameters.AddWithValue("@DateFrom", TxtDateFrom.Text);
cmd.Parameters.AddWithValue("@DateTo", TxtDateTo.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Connection = con;
try
{
con.Open();
if (DdlTickets.Items.Count > 1)
{
da.Fill(ds, strQuery);
// *** the values come from Ajax, but when It gets to fill the Gridview, it fails with error ****
GridView1.DataSource = ds.Tables[strQuery];
GridView1.DataBind();
BtnCSV.Visible = true;
}
if (GridView1.Rows.Count <= 0)
{
BtnCSV.Visible = false;
}
else
{
//Set Later
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
我尝试使用以下代码从 CalendarExtender 转换默认值,但它不起作用:
cmd.Parameters.AddWithValue("@DateFrom", DateTime.ParseExact(TxtDateFrom.Text, "yyyy-MM-ddTHH:mm:ss.mmm", CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@DateTo", DateTime.ParseExact(TxtDateTo.Text, "yyyy-MM-ddTHH:mm:ss.mmm", CultureInfo.InvariantCulture));
如果我执行以下查询而不是上面的查询,并直接获取值,它就可以工作:
"AND ([dbo].[Service].[date_entered] BETWEEN '2015-04-02T00:00:00.000' AND '2015-09-02T23:59:59.000')" +
有什么可能导致错误的想法吗?非常感谢您的帮助!
您的参数不需要使用单引号。使用单引号,您的数据库管理器会将它们视为 string literal 而不是参数。
只需删除它们。如果除此之外一切正常,它将起作用。
AND ([dbo].[Service].[date_entered] BETWEEN @DateFrom AND @DateTo)
还有一些事情;
- 使用
using
statement 自动处理您的 SqlConnection
、SqlCommand
和 SqlDataAdapter
对象,而不是手动调用 Close
或 Dispose
方法。
- 不要使用
AddWithValue
方法。 It may generate unexpected and surprising results sometimes。使用 Add
方法重载来指定参数类型及其大小。
我有休假的 SQL 查询,在获取日期值时失败:
String strQuery = @"SELECT TOP 100000" +
"[dbo].[Service].[company_name] AS 'Company Name', [dbo].[Service].[status_description] AS 'Status Description'," +
"DATEPART(month, [dbo].[Service].[date_entered]) AS 'Month', COUNT([dbo].[Service].[status_description]) AS 'Count'" +
"FROM [dbo].[Service] WITH(NOLOCK)" +
"WHERE ([dbo].[Service].[status_description] = @Status)" +
// **** I used the following to get values from Ajax CalendarExtender
// **** (Ajax Format from: yyyy-MM-ddTHH:mm:ss.mmm to: yyyy-MM-ddT23:59:59.000) *******
"AND ([dbo].[Service].[date_entered] BETWEEN '@DateFrom' AND '@DateTo')" +
"GROUP BY [dbo].[Service].[company_name], [dbo].[Service].[status_description]," +
"DATEPART(month, [dbo].[Service].[date_entered])" +
"ORDER BY [dbo].[Service].[status_description] ASC;";
System.Globalization.CultureInfo en = new System.Globalization.CultureInfo("en-US");
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(strQuery + "", con);
cmd.Parameters.AddWithValue("@Status", DdlTickets.SelectedItem.Value);
cmd.Parameters.AddWithValue("@DateFrom", TxtDateFrom.Text);
cmd.Parameters.AddWithValue("@DateTo", TxtDateTo.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Connection = con;
try
{
con.Open();
if (DdlTickets.Items.Count > 1)
{
da.Fill(ds, strQuery);
// *** the values come from Ajax, but when It gets to fill the Gridview, it fails with error ****
GridView1.DataSource = ds.Tables[strQuery];
GridView1.DataBind();
BtnCSV.Visible = true;
}
if (GridView1.Rows.Count <= 0)
{
BtnCSV.Visible = false;
}
else
{
//Set Later
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
我尝试使用以下代码从 CalendarExtender 转换默认值,但它不起作用:
cmd.Parameters.AddWithValue("@DateFrom", DateTime.ParseExact(TxtDateFrom.Text, "yyyy-MM-ddTHH:mm:ss.mmm", CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@DateTo", DateTime.ParseExact(TxtDateTo.Text, "yyyy-MM-ddTHH:mm:ss.mmm", CultureInfo.InvariantCulture));
如果我执行以下查询而不是上面的查询,并直接获取值,它就可以工作:
"AND ([dbo].[Service].[date_entered] BETWEEN '2015-04-02T00:00:00.000' AND '2015-09-02T23:59:59.000')" +
有什么可能导致错误的想法吗?非常感谢您的帮助!
您的参数不需要使用单引号。使用单引号,您的数据库管理器会将它们视为 string literal 而不是参数。
只需删除它们。如果除此之外一切正常,它将起作用。
AND ([dbo].[Service].[date_entered] BETWEEN @DateFrom AND @DateTo)
还有一些事情;
- 使用
using
statement 自动处理您的SqlConnection
、SqlCommand
和SqlDataAdapter
对象,而不是手动调用Close
或Dispose
方法。 - 不要使用
AddWithValue
方法。 It may generate unexpected and surprising results sometimes。使用Add
方法重载来指定参数类型及其大小。