[英]Conversion failed when converting date and/or time from character string
I'm trying to store a datetime
into a SQL database. 我正在尝试将
datetime
时间存储到SQL数据库中。 I use datetime2(0)
variable for that purpose. 我为此使用
datetime2(0)
变量。
But i always get this exception : 但是我总是得到这个异常:
Conversion failed when converting date and/or time from character string
从字符串转换日期和/或时间时转换失败
Here's my code that generates the error : 这是生成错误的代码:
protected void InsertDB(string title, string desc, string cat, string path)
{
string now = DateTime.Now.ToString("dd-MM-yyyy h:mm:ss tt");
title = title.Length == 0 ? "Untitled" : title;
cat = cat.Length == 0 ? "Uncategorized" : cat;
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
try
{
SqlCommand cmd = new SqlCommand(@"INSERT INTO gallery (img_title, img_desc, img_cat, img_date, img_path)
VALUES (@title, @desc, @cat, @date, @path)", con);
con.Open();
cmd.Parameters.AddWithValue("@title", title.Trim());
cmd.Parameters.AddWithValue("@desc", desc.Trim());
cmd.Parameters.AddWithValue("@cat", cat.Trim());
cmd.Parameters.AddWithValue("@date", now.Trim());
cmd.Parameters.AddWithValue("@path", path.Trim());
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
msg_lbl.Visible = true;
msg_lbl.Text = "New Image is uploaded.";
title_txt.Text = "";
desc_txt.Text = "";
cat_txt.Text = "";
}
else
{
msg_lbl.Visible = true;
msg_lbl.Text = "Error occured.";
}
}
catch (SqlException ex)
{
msg_lbl.Visible = true;
msg_lbl.Text = ex.Message; //I get this exception here
}
catch (Exception ex)
{
msg_lbl.Visible = true;
msg_lbl.Text = ex.Message;
}
}
The error must be when passing the variable "now" in the sql query. 该错误必须是在sql查询中传递变量“ now”时出现的。 If the column
Img_date
is a datetime field then you must pass the value as aa datetime not as a string. 如果列
Img_date
是日期时间字段,则必须将值作为日期时间而不是字符串传递。 Try assigning the value Datetime.Now
to the parameter @date
: 尝试将值
Datetime.Now
分配给参数@date
:
cmd.Parameters.AddWithValue("@date", DateTime.Now);
When using DateTime2
columns, you need to specifically set the parameter type - it defaults to DateTime
with AddWithValue
. 使用
DateTime2
列时,您需要专门设置参数类型-默认为带有AddWithValue
DateTime
。
Try this: 尝试这个:
SqlParameter parm = cmd.Parameters.Add("@date", SqlDbType.DateTime2);
parm.Value = DateTime.Now;
According to MSDN: 根据MSDN:
The @date parameter could map to a date, datetime, or datetime2 data type on the server.
@date参数可以映射到服务器上的date,datetime或datetime2数据类型。 When working with the new datetime data types, you must explicitly set the parameter's SqlDbType property to the data type of the instance.
使用新的datetime数据类型时,必须将参数的SqlDbType属性显式设置为实例的数据类型。 Using Variant or implicitly supplying parameter values can cause problems with backward compatibility with the datetime and smalldatetime data types.
使用Variant或隐式提供参数值可能会导致与datetime和smalldatetime数据类型向后兼容的问题。
http://msdn.microsoft.com/en-us/library/bb675168.aspx http://msdn.microsoft.com/en-us/library/bb675168.aspx
Hope this helps. 希望这可以帮助。
因为tsql不会接受'tt'格式说明符,所以出现了该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.