2009-05-27 111 views
0

我需要一些行。他們來自sql TARIH(sql列)是smalldatetime格式。但給我錯誤:DataRow [] rows = dsChart.Tables [0] .Select(「TARIH < ='」+ datestart +「」+ txtStartDateTime.Text +「'and TARIH > ='」+ dateend +「」+ txtEndDateTime的.text + 「'」);如何使用sql查詢從數據集中獲取行?

無法對System.DateTime和System.String執行'< ='操作。

dsChart = eReport.ToDataSet(); 
        if (txtStartDateTime.Text != "" && txtEndDateTime.Text != "") 
         if (ValidateHoursGap(txtStartDateTime.Text.Trim()) && 
          ValidateHoursGap(txtEndDateTime.Text.Trim())) 
         { 
          DataRow[] rows = dsChart.Tables[0].Select("TARIH>='" + datestart + " " + txtStartDateTime.Text + "' and TARIH<='" + dateend + " " + txtEndDateTime.Text+"'"); 
          dsChart.Tables[0].Rows.Clear(); 
          dsChart.Tables[0].Rows.Add(rows); 
         }
+0

這些是什麼datestart和dateend變量?在數據集表達式中,日期必須如下格式化:「#05/27/2009#」或「#05/27/2009 10:33:51#」 – 2009-05-27 12:26:21

回答

0

我的猜測是它不會將您的字符串識別爲有效的日期時間。最簡單的方法是使用適當的SQL函數將字符串轉換爲日期。

假設它是SQL2005,您需要使用CONVERT: http://msdn.microsoft.com/pt-br/library/ms187928.aspx

因此,這將類似於此: TARIH> = CONVERT(SMALLDATETIME, ' 「+ datestart + 」「 + txtStartDateTime.Text +」' ,)

其中格式是描述您的字符串使用的格式的數字,上面的鏈接有一個有效格式列表。

+0

他正在安慰一個數據集,而不是服務器,因此這不會工作。 – cjk 2009-05-27 12:41:51

0

我的猜測是它無法解析datestart + " " + txtStartDateTime.Text作爲日期時間,因此將其視爲字符串。

我會做這種轉換調用Select方法之前,然後用DateTime.ToString()傳遞日期時間格式,將與DateTime.Parse(由Select內部使用)工作。例如如果datestart格式爲 「DD/MM/YYYY」,並輸入從txtStartDateTime預計是格式爲 「HH:MM」

int hours = txtStartDateTime.Text.Substring(0,2); 
int minutes = txtStartDateTime.Text.Substring(3,2);  
DateTime dtStart = new DateTime(Int32.Parse(datestart.Substring(5)), Int32.Parse(datestart.Substring(3,2)), Int32.Parse(datestart.Substring(0,2)), Int32.Parse(hours), Int32.Parse(minutes)); 

然後調用

string dateFormat = "{0:s}"; 
DataRow[] rows = dsChart.Tables[0].Select(string.Format("TARIH >= '{0}' AND TARIH <= '{1}'"), dtStart.ToString(dateFormat), dtEnd.ToString(dateFormat)); 
相關問題