验证学生ID [英] Validate the student id

查看:96
本文介绍了验证学生ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库如下;



学生ID



1

2

3

4

5

$



表名学生。



我的设计如下;



学生ID Textbox1





在运行模式下,当我输入学生ID时,我想验证学生表中是否有该学生ID。



如果用户在Textbox1中错误输入学生ID,则显示消息输入正确的学生ID。



如何验证。



请帮帮我。



问候,

Narasiman P.

Database as follows;

Student id

1
2
3
4
5
6

Table name Student.

My design as follows;

Student id Textbox1


In run mode when i enter the Student id, i want to validate whether that student id in the Student table or not.

if user wrongly enter the student id in Textbox1, shows the message Enter Correct student id.

how can i validate.

Please help me.

Regards,
Narasiman P.

推荐答案

在文本框验证事件中,查询具有给定ID的条目的数据库(除非您已在内存中存储数据)。如果它什么也没有返回,则没有条目。如果它返回一些东西那么它是有效的。如果需要,你已经将学生信息记忆在内存中。
In the textbox validating event query the database (unless you already have the data in memory) for an entry with the given ID. If it returns nothing then there is no entry. If it returns something then it is valid. And you already have the students info in memory if you need it.


尝试:

Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT COUNT(iD) FROM myTable WHERE id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", textbox1.Text);
        int count - cmd.ExecuteScalar();
        if (count != 0)
            {
            // It exists.
            }
        else
            {
            // It doesn't exist
            }
        }
    }


试试这个







创建一个有bool返回的函数类型



public bool Validate_Id(int Id)// id = txtid.text

{

打开连接

string StrSQL =select Student(*)from Student where+ Id +;

SqlCommand cmd = new SqlCommand(StrSQL,cn);



SqlDataReader dr;

dr = cmd。 ExecuteReader();

dr.Read();

int i = 0;

if(dr.HasRows)

{

i = Convert.ToInt32(dr.GetValue) (0).ToString());

}





if(i> 0)

{

返回true;

}

其他

{

返回false;

}

}



如果返回true则id存在





希望它能帮到你
try This



Create An Function which have bool return Type

public bool Validate_Id(int Id) //id=txtid.text
{
Open The Connection
string StrSQL = "select count(*) from Student where "+Id+"";
SqlCommand cmd = new SqlCommand(StrSQL, cn);

SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
int i = 0;
if (dr.HasRows)
{
i = Convert.ToInt32(dr.GetValue(0).ToString());
}


if (i > 0)
{
return true;
}
else
{
return false;
}
}

if it return true then id Exist


Hope So It Will Help You


这篇关于验证学生ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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