如何在三层架构中传递标签

本文关键字:标签 三层 | 更新日期: 2023-09-27 18:02:57

如果用户有效与否,如何传递标签,我使用3层架构与应用程序设置。所以你能指导我如何传递标签用户是否有效吗?

     public static int login(string UserName, string Password)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = GetConnectionString();
                con.Open();
                int userid = 0;
                string selectstr = "select UserName,Password from Registration  where UserName =' " + UserName + " ' And  Password = ' " + Password + " ' ";
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = selectstr;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = con;
                userid = cmd.ExecuteNonQuery();
                cmd = null; 
                con.Close();
                return userid;
           if(......... ?)
{
labet.Text = " valid";
} else
{
label.Text = " not valid";

如何在三层架构中传递标签


userid = cmd.ExecuteNonQuery();

将给你一个对象。

你将不得不使用cmd.ExcuteReader()代替。


或者您可以使用查询

计算用户的数量。
string selectstr = "select 
                         count(UserName)                             
                     from 
                         Registration  
                      where 
                         UserName ='" + UserName + "' 
                         And  Password = '" + Password + "'";

查看计数

你的代码可以优化很多。首先,我必须告诉你,你最好使用参数、try-catch和ExecuteScalar。ExecuteScalar将返回一个单元格整数。这样你就可以验证用户的身份验证了。

编辑:

    public static void login(string UserName, string password)
    {
        SqlConnection con = new SqlConnection(GetConnectionString());
        try
        {
            con.Open();
            string selectstr = "select count(id) from Registration  where UserName=@username And  Password=@password";
            SqlCommand cmd = new SqlCommand(selectstr, con);
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            int result = cmd.ExecuteScalar();
            cmd.Dispose();
            con.Close();
            if (result == 1)
            {
                label.Text = "valid";
                //valid
            }
            else
            {
                //not
            }
        }
        catch
        {
            throw;
        }
    }