如何将 C# DateTime 变量的格式从转换d/M/yyyy HH:mm:ss
为yyyy/M/d HH:mm:ss
我不想更改字符串输出,而是更改 DateTime 变量本身。
我有一个带有@date
日期时间参数的存储过程。现在我试图将一个 DateTime 变量Today
从 C# 传递给这个存储过程。C# DateTime 变量是格式d/M/yyyy HH:mm:ss
,但存储过程需要格式yyyy/M/d HH:mm:ss
。
我尝试使用 LINQ to SQL:代码:
public static object SP_SelectSalesPriceItem()
{
var query = dc.SP_SalesPrice(DateTime.Now);
return query;
}
存储过程:
ALTER PROCEDURE [dbo].[SP_SalesPrice]
@Date datetime
AS
BEGIN
SET NOCOUNT ON;
select SalesPrice.[Item No_]
from Sales Price
where [Starting Date] < @Date and ([Ending Date] > @Date
这是我在 asp.net 中得到的错误: Specified cast is not valid。
堆栈跟踪:
[InvalidCastException:指定的转换无效。]
System.Data.SqlClient.SqlBuffer.get_Int32() +5002837
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +38
Read_SP_SalesPriceResult(ObjectMaterializer 2.MoveNext() +32 System. Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +222 System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1040 System.Web.UI. WebControls.ListView.PerformDataBinding(IEnumerable 数据)+44 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable 数据)+1281 ) +1477
System.Data.Linq.SqlClient.ObjectReader
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调) +33
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.ListView.PerformSelect() +113
System. Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.ListView.CreateChildControls() +55
System.Web.UI.Control .EnsureChildControls() +102
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(布尔型 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)+2496