开发者

Sending precise datetime to SqlDataSource as parameter

I want to add a .Net DateTime as a parameter to a SqlDataSource. I know how to add the parameter, but I need to add the DateTime as precisely as I possibly can. It needs to be very precise, since some tables use datetime columns as keys, and if the d开发者_Python百科atetime sent through is not precise enough, joins will not work correctly.

The old way (without SqlDataSource) is something like this:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "PosT2sNwu.RPT_GetFundTransactions";
cmd.Parameters.Add(new SqlParameter("@FromDate", fromDate));

It seems that the parameters for SqlDataSource only takes strings.

So the question is: How do I format the string (generated from the .Net DateTime) so that it ends up in the stored procedure as precisely (up to the milli, or even microsecond) as possible?


Use overloaded SqlParameter constructor that is using SqlDbType as parameter

SqlParameter(String, SqlDbType, Int32) 
SqlParameter(String, SqlDbType, Int32, String) 

Then use SqlDbType.DateTime described on MSDN page as "Date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds".


Is this any use?

http://msdn.microsoft.com/en-us/library/z72eefad.aspx

you should able be to call tostring on the datetime and the result will include all the parts down to the milliseconds.


You can specify the Type of the parameter either in the code behind file (like Karel suggested, specifying DateTime) or declarative by setting the Type property Type="DateTime"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜