从数据库中验证出生日期字段 [英] validate the date of birth field from the database

查看:317
本文介绍了从数据库中验证出生日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库记录中如下



名称手机Stud_DOB

Raj 9445125210 1985年12月31日

Ram 9442182455 1990年3月28日



在运行模式下如下

DOB(Radiobutton)



Text1 1985年12月31日和搜索(按钮)



名称手机Stud_DOB

Raj 9445125210 1985年12月31日



在搜索按钮中,我编写了如下代码



Sql =select stud_name as Name,convert(varchar(11),stud_dob ,106)作为DOB,stud_mobile从TB_Student移动到+ Rbapp.SelectedValue.ToString()。Trim()+= convert(varchar(11),stud_dob,106);





当我点击搜索按钮时,我输出如下



名称手机Stud_DOB

Raj 9445125210 1985年12月31日

Ram 9442182455 1990年3月28日



在搜索按钮31 De c 1985键入文本框。

gridview中的例外输出



名称Mobile Stud_DOB

Raj 9445125210 1985年12月31日





从我上面的查询中我犯了什么错误。请帮助我。



问候,

Narasiman P.

In database records as follows

Name Mobile Stud_DOB
Raj 9445125210 31 Dec 1985
Ram 9442182455 28 Mar 1990

In run mode as follows
DOB(Radiobutton)

Text1 31 Dec 1985 and Search(Button)

Name Mobile Stud_DOB
Raj 9445125210 31 Dec 1985

In search button i written the code as follows

Sql = "select stud_name as Name,convert(varchar(11),stud_dob,106) as DOB,stud_mobile as mobile from TB_Student where " + Rbapp.SelectedValue.ToString().Trim() + " = convert(varchar(11), stud_dob,106) ";


When i click the search button i got output as follows

Name Mobile Stud_DOB
Raj 9445125210 31 Dec 1985
Ram 9442182455 28 Mar 1990

In search button 31 Dec 1985 is typed in the textbox.
Excepted output as follows in gridview

Name Mobile Stud_DOB
Raj 9445125210 31 Dec 1985


from my above query what is the mistake i made.please help me.

Regards,
Narasiman P.

推荐答案

我能重现这种行为的唯一方法是选定值是列的名称 stud_dob



您可能想要使用文本框在您的查询中,而不是 Rbapp



我必须做的一点是你永远不应该使用连接字符串来创建sql语句 - 它让你对SQL注入开放。参数化查询还提供其他优势 - 请参阅 OWASP示例 [ ^ ]和 Jeff Atwood的博客 [ ^ ]
The only way I can reproduce this behaviour is if the "selected value" is the name of the column stud_dob

You probably meant to use the value from "the textbox" in your query, not whatever is in Rbapp

A point I must make is that you should never use concatenated strings to create sql statements - it leaves you open to SQL injection. Parameterised queries also offer other advantages - see the OWASP examples[^] and Jeff Atwood's blog[^] on the subject


这篇关于从数据库中验证出生日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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