我想将varchar转换为datetime [英] I want to convert varchar to datetime

查看:120
本文介绍了我想将varchar转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将varchar转换为日期时,我得到以下错误

字符串未被识别为有效的DateTime。

i尝试我创立的大多数方式但它不起作用.plz帮助我。



代码为::::

When I'm converting varchar to date time i got following error
" String was not recognized as a valid DateTime. "
i try most of the way i founded but it doesn't work.plz help me.

The code are::::

public void UpdateByPermitnoAndRegno(string _orgname, string _permitno, string _permitdate, string _regno, string _regdate, int _tob)
        {
            //string date1;
            Command = new SqlCommand("Organization_UpdateByPermitnoAndOrgno", Connection);
            Command.CommandType = CommandType.StoredProcedure;


            Command.Parameters.Add("@OrgName", SqlDbType.Char).Value = _orgname;
            Command.Parameters.Add("@Permitno", SqlDbType.Char).Value = _permitno;


            Command.Parameters.Add("@Permitdate", SqlDbType.DateTime).Value = DateTime.ParseExact(_permitdate, "yyyy/MM/dd", CultureInfo.InvariantCulture);
            Command.Parameters.Add("@Orgno", SqlDbType.Char).Value = _regno;
            Command.Parameters.Add("@Orgdate", SqlDbType.Char).Value = Convert.ToDateTime(_regdate).ToLongDateString();
            Command.Parameters.Add("@Tob", SqlDbType.Char).Value = _tob;

            Connection.Open();
            Command.ExecuteNonQuery();
            Connection.Close();
        }

推荐答案

很可能,这不是代码:它是您传递给代码的输入!

我假设此行发生错误:

Most likely, it's not the code: it's the input you are passing to the code!
I assume the error occurs on this line:
Command.Parameters.Add("@Permitdate", SqlDbType.DateTime).Value = DateTime.ParseExact(_permitdate, "yyyy/MM/dd", CultureInfo.InvariantCulture);

我首先要看看你以<$的形式处理它的确切内容c $ c> _permitdate - 如果 完全 与您指定的格式匹配: yyyy / MM / dd 然后你会得到例外。这包括字符串中的任何其他字符!



所以使用调试器:在行上放置一个断点,当你的代码命中它时,看看变量是什么包含的内容。如果它不完全匹配,那么你需要看看它来自哪里!

And I'd start by looking at exactly what you are handing it in the form of _permitdate - if it doesn't exactly match the format you specify: yyyy/MM/dd then you will get the exception. And that includes any other characters in the string!

So use the debugger: put a breakpoint on the line, and when your code hits it, look at what the variable contains. If it doesn't match exactly, then you need to look at where it came from!


我看不到什么版本的MS SQL服务器...

如果您使用的是MS SQL Server 2005及更高版本,则可以使用尝试...来解决服务器端的错误问题。 CATCH ...... [ ^ ]。



在客户端,我建议使用与sql server使用相同的文化,没有任何格式。

有看看这里:格式化特定文化的日期和时间 [ ^ ]



另一种方法是使用 SET日期格式 [ ^ ]命令。

I do not see what version of MS SQL server...
If you use MS SQL server 2005 and higher, you can hadle errors on server side by using TRY... CATCH...[^].

On client side, i'd suggest to use the same culture as sql server uses, without any formatting.
Have a look here: Formatting Date and Time for a Specific Culture[^]

Another way is to use SET DATETIME FORMAT[^] command.
SET DATETIMEFORMAT ymd;
SELECT <field_list>
FROM <table_name>
WHERE DateTimeField BETWEEN @startdate AND @enddate</table_name></field_list>


这篇关于我想将varchar转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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