2013-01-02 30 views
1

我在我的代碼中有一個DateTime組件,我想用它在我的SQL Server數據庫中進行查詢。從.NET的DateTime到SQL中的smalldatetime - 如何查詢?

插入此組件時,似乎沒有問題,但在查詢smalldatetime值時,我只是不知道該怎麼做。數據集總是空的。

cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "ReadDates"; 

dataset = new DataSet(); 

SqlParameter parameter = new SqlParameter("@date", SqlDbType.SmallDateTime); 
parameter.Value = DateTime.Now(); 
cmd.Parameters.Add(parameter); 

dataAdapter = new SqlDataAdapter(cmd); 
dataAdapter.Fill(dataset); 
return dataset; 

而且這是在我的存儲過程:

select * from TableDates 
where ValueDate <= @date 

所以我必須運行在SQL Server Management Studio中的程序沒有問題,進入此格式的參數時:'2000-03-03 04:05:01',但路過時, DateTime,查詢總是空的。有什麼建議麼?

+4

你能證明你的*完整的價值*存儲過程聲明,包括參數聲明? –

+1

或者,如果您使用的是SQL 2012,則可以'爲'ValueDate'和@ @ date'CAST(@DATE AS DATE)'確保時間組件被剝離。 –

+8

@ CR41G14:這可能是最糟糕的**建議!如果你有'DateTime' - 你應該儘可能地使用**本地**日期/時間數據類型**避免**來回轉換爲字符串! –

回答

-2

因爲你的參數包括日期和月份部分零... SQL Server的轉換,但所行的匹配你的日期....即

如果DATETIME.now()返回「2000年3月3日04:05:01'...它被鑄造成2000-3-3不包括零...所以你需要指定零也符合你的日期。

+0

「零」?我不明白這與它有什麼關係。 – Gabe

1

我嘗試了使用SQL Server 2008 R2 Express。

下面是示例存儲過程我寫道:

CREATE PROCEDURE [dbo].[ShowGivenSmallDateTimeValue] 
    @givenSmallDateTime smalldatetime 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Simply return the given small date time value back to sender. 
    SELECT @givenSmallDateTime 
END 

這裏是C#代碼執行過程:

var connectionBuilder = new SqlConnectionStringBuilder(); 
connectionBuilder.DataSource = "localhost\\sqlexpress"; 
connectionBuilder.IntegratedSecurity = true; 

var now = DateTime.UtcNow; 

using (var connection = new SqlConnection(connectionBuilder.ConnectionString)) 
using (var command = new SqlCommand()) 
{ 
    command.Connection = connection; 
    command.CommandType = CommandType.StoredProcedure; 
    command.CommandText = "ShowGivenSmallDateTimeValue"; 
    command.Parameters.Add(new SqlParameter("@givenSmallDateTime", SqlDbType.SmallDateTime) { Value = now }); 

    connection.Open(); 
    var result = (DateTime)command.ExecuteScalar(); 
    var difference = result - now; 

    Console.WriteLine("Due to the smalldatetime roundings we have a difference of " + difference + "."); 
} 

而且這只是工作。

+0

所以看起來不同的是,你永遠不會顯式地將參數的'DBType'設置爲'SqlDbType.SmallDateTime'。如果補充一點,它會失敗嗎? – mbeckish

+0

@mbeckish:剛剛採用我的示例代碼,結果沒有任何區別。 – Oliver

0

這裏是我爲Datetime創建SqlParameter的代碼;對於SQL Server 2008,我們通過爲DATETIME2因爲SQL會含蓄從DATETIME2,只要它是轉換爲所有其他日期類型目標類型的範圍內......

  // Default conversion is now DateTime to datetime2. The ADO.Net default is to use datetime. 
      // This appears to be a safe change as any datetime parameter will accept a datetime2 so long as the value is within the 
      // range for a datetime. Hence this code is acceptable for both datetime and datetime2 parameters, whereas datetime is not 
      // (because it doesn't handle the full range of datetime2). 
      SqlParameter sqlParam = new SqlParameter(name, SqlDbType.DateTime2);