将数据类型nvarchar转换为c#中的datetime时出错 [英] error converting data type nvarchar to datetime in c#

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

问题描述

我开发了一个应用程序,我在桌面上工作得非常好,但是当我在笔记本电脑上安装它时会在保存操作期间生成错误消息,请告诉我在我的笔记本电脑中需要什么设置..谢谢。

I have develop an application which i work very well on desktop but when i install it on laptop it will generate and error msg during save operation please suggest me what setting is require in my laptop.. thanks.

推荐答案

如果你收到这个确切的消息,那么它来自SQL:

If you are getting this exact message, then it is coming from SQL:
error converting data type nvarchar to datetime



这意味着您将应用程序中的日期作为字符串传递给SQL - 可能是通过连接字符串形成SQL查询(因为您没有在你的台式机上得到错误,我认为日期输入源是相同的)



这是一个糟糕的想法在最好的时候 - 它离开你对SQL注入攻击持开放态度 - 并导致这样的问题,因为两台计算机上的默认日期时间格式设置不同。

在整个过程中使用参数化查询,并将DateTime值作为DateTime传递(a与其本机数据类型中的所有其他数据一起使用而不是转换它。您的问题将会消失,并且您的代码可能会降低用户意外或故意破坏您的数据库的风险。


And what it means is that you are transferring a date from your app to SQL as a string - probably by concatenating strings to form an SQL query (since you don't get the error on your desktop machine, and I assume that the date input source is the same)

This is a poor idea at the best of times - it leaves you wide open to SQL injection attacks - and causes problems like this because the default datetime format is set differently on the two computers.
Use parametrized queries throughout, and pass the DateTime value as a DateTime (along with all other data in their native datatypes) instead of converting it. Your problem will go away, and your code will be at less risk of a user accidentally or deliberately destroying your database.


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

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