简体   繁体   English

从字符串转换日期和/或时间时转换失败

[英]Conversion failed when converting date and /or time from character string

Error is appearing in date_to and date_from while adding data into the database. 将数据添加到数据库时,date_to和date_from中出现错误。 In sql server database the data type for date_to & date_from are date. 在sql server数据库中,date_to和date_from的数据类型是date。 suggest some solution. 建议一些解决方案

try
{
   cmd = new SqlCommand("insert into license1 values(@l_id,@customer_id,@d_id,@device_name,@from,@to)", cn);

   cmd.Parameters.AddWithValue("@l_id", license_id.Text);
   cmd.Parameters.AddWithValue("@customer_id", c_comboBox4.Text);
   cmd.Parameters.AddWithValue("@d_id", d_id_comboBox4.Text);
   cmd.Parameters.AddWithValue("@device_name", d_name_comboBox5.Text);
   cmd.Parameters.AddWithValue("@to", date_to.Text);
   cmd.Parameters.AddWithValue("@from", date_from.Text);

   cn.Open();
   a = cmd.ExecuteNonQuery();
   if (a > 0)
   {
      MessageBox.Show("Data Submitted");
   }

 }
 catch (Exception ex)
 {
      MessageBox.Show(ex.Message);
 }

Take the conversion into your own hands: 转换到自己手中:

cmd.Parameters.AddWithValue("@to", DateTime.Parse( date_to.Text));
cmd.Parameters.AddWithValue("@from", DateTime.Parse( date_from.Text));

and when that still fails, use a version of DateTime.ParseExact() with an appropriate Format and CultureInfo. 当仍然失败时,使用具有适当的Format和CultureInfo的DateTime.ParseExact()版本。

You might want to consider adding a more robust and extensive validation layer. 您可能需要考虑添加更强大和更广泛的验证层。 Dates and numbers are very sensitive to typing errors, User settings and User assumptions. 日期和数字对输入错误,用户设置和用户假设非常敏感。

Always assume TextBox.Text is full of errors. 总是假设TextBox.Text充满了错误。

if user enters the Date Format as: yyyy-MM-dd then try this: 如果用户输入日期格式为: yyyy-MM-dd尝试以下操作:

String strDateFormat= "yyyy-MM-dd";//change accordingly if format is something different
DateTime to=DateTime.ParseExact(date_to.Text,strDateFormat, CultureInfo.InvariantCulture);    
DateTime from=DateTime.ParseExact(date_from.Text, strDateFormat, CultureInfo.InvariantCulture);

cmd.Parameters.AddWithValue("@to", to);
cmd.Parameters.AddWithValue("@from", from);

try this 尝试这个

 cmd.Parameters.AddWithValue("@to",Convert.ToDateTime(date_to.Text));
 cmd.Parameters.AddWithValue("@from",Convert.ToDateTime( date_from.Text});

I think your date format may caused the issue. 我认为您的日期格式可能会导致问题。 You must use date format supported by the SQL provider. 您必须使用SQL提供程序支持的日期格式。 Most SQL uses MM-dd-yyyy format. 大多数SQL使用MM-dd-yyyy格式。 So if you pass 21-1-2014, SQL server don't accept it because 21st month don't exist. 因此,如果你通过21-1-2014,SQL服务器不接受它,因为21个月不存在。

If the problem persists while executing your code, perhaps it is due to two important reasons: 如果在执行代码时问题仍然存在,可能是由于两个重要原因:

  1. You have used the DateTime.Now.ToShortString or similar auto conversion method, and 您已使用DateTime.Now.ToShortString或类似的自动转换方法,和
  2. You changed the computers default Datetime format in the setting. 您在设置中更改了计算机的默认日期时间格式。

Change the Datetime format of the computer or change the autoconversion to 'parse' method. 更改计算机的日期时间格式或将自动转换更改为“解析”方法。

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

相关问题 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and /or time from character string 从字符串错误4转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string error 4 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 错误“从字符串转换日期和/或时间时转换失败” - Error “Conversion failed when converting date and/or time from character string” 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 问题:从字符串转换日期和/或时间时转换失败 - Problem: Conversion failed when converting date and/or time from character string 从字符串#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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM