尝试从.net插入日期时出现日期格式问题 [英] Date Format issue while try to insert date from .net

查看:95
本文介绍了尝试从.net插入日期时出现日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sql server 2008和.net 2.0.我的Sql Server具有MM/DD/yyyy格式

当我尝试使用查询分析器插入日期数据时,它需要mm/dd/yyyy格式.但是当我使用.net代码将日期数据作为过程格式传递时,我需要更改为dd/mm/yyyy格式来插入数据至于查询从.net提交的日期,则需要更改dd/mm/yyyy格式以获取数据,否则会引发错误.为什么我需要更改日期格式.任何人都可以为其回答...

解决方案

.NET中的日期格式取决于CurrentCulture ,除非有所不同在产生DateTime value的字符串输出的方法中指定了FormatProvider .
在下面的代码中,前两个打印的DateTime 值由CurrentCulture 控制,而在第三个中,作为ToString 方法的参数的FormatProvider 覆盖了CurrentCulture 格式.

今天的DateTime = DateTime.Now;
Thread.CurrentThread.CurrentCulture =
        System.Globalization.CultureInfo.GetCultureInfo(" );
Console.WriteLine(今天);

Thread.CurrentThread.CurrentCulture =
        System.Globalization.CultureInfo.GetCultureInfo(" );
Console.WriteLine(today.ToString());

Console.WriteLine(today.ToString(System.Globalization.CultureInfo
        .GetCultureInfo(" ).DateTimeFormat)));

//  2012年5月15日15:56:38 
//  2012/5/15下午3:56:38 
//  2012年5月15日15:56:38  



请尝试在您的代码上下文中解释以上信息.我认为这可能会有所帮助.


解决方案1很好.

我认为您可以使用设置日期格式 [ 解决方案

The Date Format in .NET depends upon the CurrentCulture unless a different FormatProvider is specified in the method producing the string output of DateTime value.
In the following code the first two printed DateTime values are governed by the CurrentCulture whereas in the third the FormatProvider given as an argument to ToString method has overridden the CurrentCulture format.

DateTime today = DateTime.Now;
Thread.CurrentThread.CurrentCulture = 
        System.Globalization.CultureInfo.GetCultureInfo("hi-IN");
Console.WriteLine (today);

Thread.CurrentThread.CurrentCulture = 
        System.Globalization.CultureInfo.GetCultureInfo("en-US");
Console.WriteLine (today.ToString());

Console.WriteLine (today.ToString(System.Globalization.CultureInfo
        .GetCultureInfo("hi-IN").DateTimeFormat));

//15-05-2012 15:56:38
//5/15/2012 3:56:38 PM
//15-05-2012 15:56:38



Please try to interpret the above info in the context of your code. I think it may be helpful.


Solution no 1 is very good.

I think you can use
SET DATEFORMAT[^] command to change date format for SQL Server.


这篇关于尝试从.net插入日期时出现日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆