0
我有休耕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')" +
任何可能導致錯誤的想法?非常感謝您的幫助!
太棒了!非常感謝。我掙扎了幾個小時,卻弄不明白。有效! – Jacman