简体   繁体   English

从字符串转换日期和/或时间时如何修复转换失败

[英]How do I fix Coversion failed when converting date and/or time from character string

con.open();
SqlCommamd comm = new SqlCommand("Insert into Debt_Tab values('"+Textbox1.text+"')",con);
comm.ExecuteNonQuery();

Textbox1 I is declared as a DateTime in my Sql table. Textbox1 I 在我的 Sql 表中被声明为DateTime

There are a lot of different ways to format a date.格式化日期有很多不同的方法。 To be sure that the database gets the correct format I suggest that you parse the date by specifying a culture.为了确保数据库获得正确的格式,我建议您通过指定文化来解析日期。

For desktop applications, this is easy since the OS is configured for a specific format, while for web applications the user uses their own preferred format (unless you have specified something else).对于桌面应用程序,这很容易,因为操作系统是针对特定格式配置的,而对于 Web 应用程序,用户使用他们自己喜欢的格式(除非您指定了其他格式)。

To parse using the OS culture:要使用 OS 文化进行解析:

var date = DateTime.Parse(Textbox1.Text)

To parse using a specific culture:要使用特定文化进行解析:

var swedish = new CultureInfo("sv_se");
var date = DateTime.Parse(TextBox1.Text, swedish);

Another thing.另一件事。 There is something seriously wrong with your code.您的代码存在严重问题。 It's vulnerable to SQL injection attacks.它容易受到 SQL 注入攻击。 You need to use a parameterized query instead.您需要改用参数化查询。

var cmd = new SqlCommand(con);
cmd.CommandText = "Insert into Debt_Tab values(@date)";
cmd.Parameters.AddWithValue("date", date);
cmd.ExecuteNonQuery();

使用这个希望这会奏效

DateTime.ParseExact(Textbox1.text, "MM/dd/yyyy", CultureInfo.InvariantCulture)

Try this:-尝试这个:-

Convert.ToDateTime()

example:-例子:-

con.open();
SqlCommamd comm = new SqlCommand("Insert into Debt_Tab values('"+ Convert.ToDateTime(Textbox1.text).ToString("mm/dd/yyyy") +"')",con);
comm.ExecuteNonQuery();

Try the following way to validate the date尝试以下方式来验证日期

bool res;
  DateTime Date;
  string myDate = Textbox1.text;
  res = DateTime.TryParse(myDate, out Date);
  if(res)
  {
   // Validation or conversion success and result will be available in Date variable
  }
  else 
  {
     // conversion Fail
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 SqlError :从字符串转换日期和/或时间时失败。 EF 二进制表达式? - how to fix SqlError :failed when converting date and/or time from character string. EF binaryexpression? 从字符串转换日期和/或时间时,如何转换失败 - How to Conversion failed when converting date and/or time from character string 错误:当我选择日期时,从字符串转换日期和/或时间时转换失败? - error: Conversion failed when converting date and/or time from character string when i select date? 从字符串#2转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string #2 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 从Winforms中的字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string in winforms 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 错误:从字符串转换日期和/或时间时转换失败 - Error : Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM