English 中文(简体)
如何将借记日期格式从C#改为ql后继
原标题:How do i pass arbitary date format from C# to sql backend

我在交易日期后有一个时间点。 因此,我从C#.net前,以以下格式通过:

2011-01-01 12:17:51.967

我写道:

介绍层:

string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
PropertyClass prp=new PropertyClass();
Prp.TransDate=Convert.ToDateTime(date);

财产 班级结构:

Public class property
{
private DateTime transdate;
public DateTime TransDate
{
    get
    {
        return transdate;
    }
    set
    {
        transdate = value;
    }
   }
}

从DAL层穿过“交易”这样的交易:

Cmd.Parameters.AddWithValue("@TranSactionDate”, SqlDbType.DateTime).value=propertyobj.TransDate;

草原层的分解:

string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);

我正在获得正确的预期日期格式,但当辩论转向这一行文时。

Prp.TransDate=Convert.ToDateTime(date);

日期改为2011年1月1日。

But my backend sql datefield wants the date paramter 2011-01-01 12:17:51.967 in this format otherwise throwing exception invalid date format.

www.un.org/Depts/DGACM/index_spanish.htm 注:虽然时间过长,但并未将例外情形改为:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753     12:00:00 AM and 12/31/9999 11:59:59 PM. at   System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value) at System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value) at System.Data.SqlTypes.SqlDateTime..ctor(DateTime value) at System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb) at System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Byte scale, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc) 
问题回答

你说:

 So I am passing that date from front C#.net, in the below format:

但是,我看不出任何证据,证明你完全采用任何格式,实际上,你是shouldn t<>。 ADatetime 没有任何固有的形式,比数字多。 就像你能够看到同样数目的血压或血清一样,你可以以各种方式看待同一日期和时间。

您已经将<代码>Datetime直接通过,而不是以具体格式通过,这是正确的(通常,关于日期/时间格式和数据库的问题涉及的是没有使用参数查询的人)。 你们不必担心这种形式。

可能性:

  • Your data is getting corrupt elsewhere, and you ve actually got a date before 1753
  • Your SQL statement is using the wrong value
  • There s something wrong with your database driver (you haven t said which database you re using; is it SQL Server?)

1. 直接通过作为参数的审理日期:

Cmd.Parameters.AddWithValue("@TranSactionDate", DateTime.Now);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签