将数据类型nvarchar转换为数字时出错。 [英] Error converting data type nvarchar to numeric.

查看:560
本文介绍了将数据类型nvarchar转换为数字时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预郎= C#> SqlConnection的SC = <跨度类= 代码关键字>新的SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings [<跨度类= 代码串 > hospitalmanagementdb]。ToString());
SqlCommand cmd = new SqlCommand( insert进入病房值((从患者中选择(patient_id)),' + ','1' + System.DateTime.Today.Date.ToString()+ ','admit' + ','paid'),sc);

cmd.Connection = sc;
sc.Open();
cmd.ExecuteNonQuery();
sc.Close();





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





帮助我解决这个错误

解决方案

可能你有一个ID列,即身份并且您没有指定它 - 而且您不能 - 但是因为您没有指定要将数据加载到SQL中的哪些列尝试按内部列顺序设置它们。

命名列!

  INSERT   INTO  MyTable(col1 ,col2) VALUES  1  2 

并且您的问题可能会消失...


您应该知道在代码中使用查询是一种不好的做法,尤其是当您使用ASP时。 NET程序员。



我们多少次警告会员SQL注射 [ ^ ]?我不知道。请按照链接学习最佳实践。




尝试在具有数值数据类型的列中插入字符时发生此异常

快乐编码!

:)


SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
            SqlCommand cmd = new SqlCommand("insert into ward values((select (patient_id) from patient),'" + "','1'" + System.DateTime.Today.Date.ToString()+ "','admit'"+ "','paid')", sc);
           
            cmd.Connection = sc;
            sc.Open();
            cmd.ExecuteNonQuery();
            sc.Close();



error: Error converting data type nvarchar to numeric.


help me to solve this error

解决方案

Probably, you have an ID column, that is identity and you are not specifying it -and you can't- but because you don't specify which columns you want to load data into SQL tries to set them in internal column order.
Name your columns!

INSERT INTO MyTable (col1, col2) VALUES (1, 2)

and your problem will probably go away...


You should know that using queries in code behind is a bad practice, especially when you're ASP.NET programmer.

How many times we have warned members about SQL injection[^]? I don't know. Please, follow the link and learn best practices.


Hi,
This exception occures when try to insert characters in a column having numeric datatype
Happy Coding!
:)


这篇关于将数据类型nvarchar转换为数字时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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