[不是问题!]从字符串“pao”转换输入'日期'无效。 [英] [NOT a question!] Conversion from string "pao" to type 'Date' is not valid.

查看:85
本文介绍了[不是问题!]从字符串“pao”转换输入'日期'无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字符串pao到Date类型的转换无效。

解决方案

好吧,不,它不会。

日期格式为

 yyyy-mm-dd 2014-01-08 
dd / mm / yy 08/01/14
mm / dd / yyyy 01/08/2014
dd / mmm / yyyy 01 / JAN / 2014

pao这个词不是约会对象。



猜测,你是从VB传递数据到SQL,你做错了两件事:

1)你不应该将字符串连接起来您的SQL命令 - 它让您对SQL注入完全开放,这可能会损坏或破坏您的数据库。请改用参数化查询。

2)您还应验证用户输入,以确保它们是真正的日期,然后再尝试将任何内容传递给SQL - 用户会犯错误,如果您不进行检查,那么当您的应用程序崩溃是因为用户输错了一个字母而不是一个数字,他们会对你很恼火。



使用DateTime.TryParse将用户输入转换为DateTime值,如果不能报告错误,并通过参数化查询将DateTime值传递给SQL


Conversion from string "pao" to type 'Date' is not valid.

解决方案

Well, no, it wouldn't be.
A date is of the form

yyyy-mm-dd     2014-01-08
dd/mm/yy       08/01/14
mm/dd/yyyy     01/08/2014
dd/mmm/yyyy    01/JAN/2014

And the word "pao" is not a date.

At a guess, you are passing data to SQL from your VB, and you are doing two things wrong:
1) You should not concatenate strings to form your SQL command - it leave you wide open to SQL Injection which can damage or destroy your database. Use parameterised queries instead.
2) You should also validate your user inputs to ensure they are real dates before trying to pass anything to SQL - users make mistakes, and if you do not check, then when your application crashes because a user mistyped a letter instead of a digit they are going to be rather annoyed with you.

Use DateTime.TryParse to convert the user input to a DateTime value, report an error if it can't, and pass the DateTime value to SQL via a parameterized query.


这篇关于[不是问题!]从字符串“pao”转换输入'日期'无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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