错误:将数据类型varchar转换为数字时出错

时间:2022-05-14 15:41:59

I am trying to insert into the database a string of the current time:

我试图在数据库中插入当前时间的字符串:

string tm = DateTime.Now.ToString("HH:mm:ss");
string sql = string.Format("INSERT INTO Kabala1 (Nu_kabala,Ma_num,Date,Time,Total,Status,Name,User_n) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", n, Session["Ma_num"], now, tm, lprice, "sdfsdf", Session["user"], "sdfsdf");

But when I run the code above I get the error: error converting data type varchar to numeric.

但是当我运行上面的代码时,我得到错误:将数据类型varchar转换为数字时出错。

How do I solve it?

我该如何解决?

1 个解决方案

#1


0  

One or more of the column data types that you're inserting into is numeric, however you are wrapping all of the values you insert in single quotes ('). This means that when the value is inserted, it will try to cast an varchar (the value you wrapped in single quotes) as a numeric value, and fail.

您要插入的一个或多个列数据类型是数字,但是您将所有插入的值包装在单引号(')中。这意味着当插入值时,它将尝试将varchar(包装在单引号中的值)转换为数值,并失败。

You need to identify which of the values you are trying to insert is numeric, and remove the single quotes from it within the insert statement.

您需要确定要尝试插入的值是数字,并在insert语句中从中删除单引号。

For example:

string.Format("INSERT INTO Kabala1 (Nu_kabala,Ma_num,Date,Time,Total,Status,Name,User_n) VALUES('{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}')", n, Session["Ma_num"], now, tm, lprice, "sdfsdf", Session["user"], "sdfsdf");

Where {4} is numeric.

其中{4}是数字。

#1


0  

One or more of the column data types that you're inserting into is numeric, however you are wrapping all of the values you insert in single quotes ('). This means that when the value is inserted, it will try to cast an varchar (the value you wrapped in single quotes) as a numeric value, and fail.

您要插入的一个或多个列数据类型是数字,但是您将所有插入的值包装在单引号(')中。这意味着当插入值时,它将尝试将varchar(包装在单引号中的值)转换为数值,并失败。

You need to identify which of the values you are trying to insert is numeric, and remove the single quotes from it within the insert statement.

您需要确定要尝试插入的值是数字,并在insert语句中从中删除单引号。

For example:

string.Format("INSERT INTO Kabala1 (Nu_kabala,Ma_num,Date,Time,Total,Status,Name,User_n) VALUES('{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}')", n, Session["Ma_num"], now, tm, lprice, "sdfsdf", Session["user"], "sdfsdf");

Where {4} is numeric.

其中{4}是数字。