检查用户名是否在我的数据库中的条件 [英] condition to check if username in my database or not

查看:48
本文介绍了检查用户名是否在我的数据库中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
可以帮助我!
我是初学者,使用C#:)

请问!!
我如何从数据库中获取列并对特定列中的每一行进行检查以检查该用户是否存在!
非常感谢:)
这是我的代码:

hi
can help me !!
I''m beginner programmer use C# :)

can i ask!!
how can i get column from my database and compere each rows in specific column to check if this user exists or not !
Thanks Alot :)
this my code:

 public SqlDataReader searchMethod(string searchCriteria,string lebel)
    {
        SqlDataReader MyReader;
        //-------------------------------------------------------------------
        //1)perpare number:
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "server=xxx; database=Test; integrated security=true";
        //==========================================================
        //2)prepare request:
            SqlCommand cm = new SqlCommand();
            cm.Connection = cn; // to determine the connection name
            cm.CommandType = CommandType.Text;
            cm.CommandText = "SELECT Emp_id,Emp_Name FROM Table1 where Emp_id like ''" + searchCriteria + "''"; // to determine connection text
       //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       // ----- Check whether the name already exists in the database: ----
        // check whether the name is empty

            if (searchCriteria.Equals(""))
            {
                lebel = "Please enter a desired Username";
            }
            else if(checkexists())
            {
                lebel = "Happy";
            }
            else
            {
                lebel = "Sad";
            }
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//if the user exists insert Password into specific User
            cm.CommandText = "INSERT INTO Table1 (Password) VALUES (@Password)";
            cm.Parameters.Add("@Password");
        //==========================================================
        //3)open connection:
            cn.Open();
        //==========================================================
        //4)execute the commands:
            MyReader = cm.ExecuteReader(); // load data into temp memory
            return MyReader; 
        //==========================================================
        //5)close connectio
        cn.Close(); 
    }





private Boolean checkexists()
       {
           //1)perpare number:
           SqlConnection cn = new SqlConnection();
           cn.ConnectionString = "server=XXX; database=Test; integrated security=true";
           //---------------------------------------------------------------
           SqlCommand cmd = new SqlCommand("Select Count(*)  from Table1 where Emp_id = @Emp_id",cn);
           SqlDataReader sReader = null;
           Int32 numberOfRows = 0;
           try
           {
               cn.Open();
               sReader = cmd.ExecuteReader();

               while (sReader.Read())
               {
                   if (!(sReader.IsDBNull(0)))
                   {
                       numberOfRows = Convert.ToInt32(sReader[0]);
                       if (numberOfRows > 0)
                       {
                           return true;
                       }
                   }
               }
           }
           catch (Exception ex)
           {
           }
           finally
           {
               cn.Close();
           }
           return false;
       }
 }

推荐答案

有很多奇怪的地方!
我认为,在继续之前,您需要回顾一下课程笔记,因为该代码太错误了.

1)您准备请求"-但不要使用它...
2)您检查名称是否存在"-但无论如何都尝试将其插入.
3)SQL INSERT语句创建新记录,而不更新现有记录.因此,您需要提供其他用户信息才能在其中写入密码.
4)您不使用读取器来更新数据库,而是使用读取器来读取数据库.
5)请找到比SQLDataReader更好的返回值-用户可能会很好,如果可能有多个条目,甚至可能是DataTable或DataSet.
6)如果要返回Count,则不需要读取器,可以改用ExecuteScalar,它返回一个数字.
7)如果指定参数,则必须提供参数值!使用SqlCommand.Parameters.AddWithValue进行此操作.

确实,您需要从头开始,并确定要尝试的工作,然后再着手编写代码!
There are a number of oddities there!
I think before you go any further, you need to look back at your course notes, because that code is just soo wrong.

1) You "prepare request" - but then don''t use it...
2) You "Check if the name exists" - but then try to insert it anyway.
3) An SQL INSERT statement creates a new record, not updates an existing. So you would need to supply the other user info to write the password in.
4) You don''t use a reader to update a database, you use a reader to read from a database.
5) Please, find something better to return than an SQLDataReader - a User would probably be nice, or even a DataTable or DataSet if there may be multiple entries.
6) If you are returning a Count, you don''t need a reader, you can use ExecuteScalar instead, and it returns a number.
7) If you specify a parameter, you have to supply the parameter value! Use SqlCommand.Parameters.AddWithValue to do that.

Really, you need to start from scratch, and work out what you are trying to do before rushing into code!


这篇关于检查用户名是否在我的数据库中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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