字符串未被识别为有效的DateTime phpMyAdmin [英] String was not recognized as a valid DateTime phpMyAdmin

查看:152
本文介绍了字符串未被识别为有效的DateTime phpMyAdmin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用phpMyAdmin,并希望从C#更新数据。



我有yyyy-MM-dd格式为phpMyAdmin中的列 dateBuy 格式



要显示它,我将格式更改为



这是代码:(我没有这个代码的问题)

  string dateBuy = dr.GetValue(1).ToString(); 
DateTime time = DateTime.Parse(dateBuy);
dateBuy = time.ToString(dd / MM / yyyy);

要更新数据库,我想将格式更改回yyyy- MM-dd。但是,我有一个错误:字符串未被识别为有效的DateTime



这是错误我的代码部分:

  string dateBuy2 = txtDateBuy.Text; 
dateBuy =(DateTime.ParseExact(dateBuy2,yyyy-MM-dd,null))。ToString();

我在DateTime语法中有错误吗?感谢您的帮助。

解决方案

[既然如果我们继续在评论中不是很好(评论将会很长)我将把我所想到的解决方案放在这里]



要将 dateBuy 格式化为您想要的格式你还应该把字符串格式放在 ToString()



中,而不是

  dateBuy =(DateTime.ParseExact(dateBuy2,yyyy-MM-dd,null))ToString(); 

put

 c $ c> dateBuy =(DateTime.ParseExact(dateBuy2,yyyy-MM-dd,null))ToString(yyyy-MM-dd); 

否则,可以使用 ToString()产生类似2015-10-16 12:00:00 AM而不是2015-10-16



但是,由于您使用 ParseExact dateBuy2 的输入格式也必须采用yyyy-MM-dd的格式,这样就不符合目的。您可以考虑使用 DateTimePicker ,以便您可以控制输入格式。



或者,您可以使用 DateTime.Parse TryParse (由Martheen建议),而不是这样的



{pre> try {
DateTime dt = DateTime.Parse(txtDateBuy.Text);
dateBuy = dt.ToString(yyyy-MM-dd);
} catch(异常exc){
//错误的格式,做某事告诉用户
}

如果输入必须在 TextBox 中,最好放入 try-catch 如果您使用 Parse 来防止程式错误输入格式错误。



如果您使用 TryParse ,则可以将其放在 if-else 块语句

  DateTime dt; 
if(DateTime.TryParse(txtDateBuy.Text,CultureInfo.InvariantCulture,DateTimeStyles.AssumeLocal,out dt)){
//正确的格式,使用dt
} else {
//格式不正确,警告用户
}

要获取 CultureInfo 枚举您需要添加对 System.Globalization的引用



[由Soner Gonul先生提出建议后编辑]


Worked with phpMyAdmin, and wanted to update data from C#.

I had "yyyy-MM-dd" format for column dateBuy in phpMyAdmin.

To display it, I changed the format to "dd/MM/yyyy" in C#.

This is the code: (I don't have problem with this code)

string dateBuy = dr.GetValue(1).ToString();
DateTime time = DateTime.Parse(dateBuy);
dateBuy = time.ToString("dd/MM/yyyy");

To update database, I wanted to change the format back to "yyyy-MM-dd". But, I had an error: "String was not recognized as a valid DateTime".

This is the error parts of my code:

string dateBuy2 = txtDateBuy.Text;
dateBuy = (DateTime.ParseExact(dateBuy2, "yyyy-MM-dd", null)).ToString();

Did I make mistake in the DateTime syntax? Thanks for your help.

解决方案

[Since it is not good if we continue in the comments (the comments will be long), I will just put up what I think as a solution here]

To format dateBuy to the format that you want, you should also put the string format in the ToString()

That is, instead of

dateBuy = (DateTime.ParseExact(dateBuy2, "yyyy-MM-dd", null)).ToString();

put

dateBuy = (DateTime.ParseExact(dateBuy2, "yyyy-MM-dd", null)).ToString("yyyy-MM-dd");

Otherwise, it is possible for the ToString() to produce something like "2015-10-16 12:00:00 AM" instead of "2015-10-16"

However, since you use ParseExact, the input for the dateBuy2 must also be in the format of "yyyy-MM-dd" which defeats the purpose. You may consider using DateTimePicker such that you can control the input format.

Alternatively, you can use DateTime.Parse or TryParse (as suggested by Martheen) instead, something like this

try {
    DateTime dt = DateTime.Parse(txtDateBuy.Text);
    dateBuy = dt.ToString("yyyy-MM-dd");
} catch (Exception exc) {
    //wrong format, do something to tell the user
}

If input has to be in the TextBox you better put try-catch to prevent your program crash for taking wrong-formatted input if you use Parse.

Where as if you use TryParseyou can put it in if-else block statement instead

DateTime dt;
if (DateTime.TryParse(txtDateBuy.Text, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt)) {
    //correct format, do something using dt
} else {
    //incorrect format, warns the user
}

To get CultureInfo enum you need to add reference to System.Globalization

[Edited after suggestion given by Mr. Soner Gonul]

这篇关于字符串未被识别为有效的DateTime phpMyAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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