首页 > 解决方案 > .net framework 3.5 登录表单代码不起作用

问题描述

我最近在 .net framework 4.7.2 中开发了一个 pos 系统,该应用程序运行良好并安装在客户端机器中。目前我正在为.net framework 3.5 中的另一个客户端开发管理系统我创建了数据库并开始编写登录表单,我从我的 pos 系统中获取现有代码更改了连接字符串一切都很顺利但是当我点击登录按钮没有任何反应,下一个表单不显示。这是代码

if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox1.Focus();
            return;
        }
        if (textBox2.Text == "")
        {
            MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox2.Focus();
            return;
        }
        string cs = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=filali;Integrated Security=True";
        SqlConnection myConnection = default(SqlConnection);
        myConnection = new SqlConnection(cs);

        SqlCommand myCommand = default(SqlCommand);

        myCommand = new SqlCommand("select * from dbo.[login] where [username] = @user and password = @pass", myConnection);

        SqlParameter uName = new SqlParameter("@user", SqlDbType.VarChar);
        SqlParameter uPassword = new SqlParameter("@pass", SqlDbType.VarChar);

        uName.Value = textBox1.Text;
        uPassword.Value = textBox2.Text;

        myCommand.Parameters.Add(uName);
        myCommand.Parameters.Add(uPassword);

        myCommand.Connection.Open();
        try
        {
            

            SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            if (myReader.Read() == true)
            {
                
                    string role = myReader.GetString(3).ToString();
                Console.WriteLine(myReader.GetString(3).ToString());
                    switch (role)
                    {
                        case "a":
                        Console.WriteLine("this is admin "+role);
                        mdiparent1 mdiparent1 = new mdiparent1();
                            this.Hide();
                            mdiparent1.Show();
                     

                            break;
                        case "u":
                            sortiev2 instance = new sortiev2();
                            this.Hide();
                            instance.Show();

                            if (instance.FormBorderStyle == FormBorderStyle.None)
                                instance.FormBorderStyle = FormBorderStyle.Sizable;

                            instance.BringToFront();

                            if (instance.WindowState == FormWindowState.Maximized)
                                instance.WindowState = FormWindowState.Normal;



                            break;
                    }

                
            }

            else
            {
                MessageBox.Show("Invalid username or password");
            }

            if (myConnection.State == ConnectionState.Open)
            {
                myConnection.Dispose();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

sql 连接正在工作,因为“角色”记录在控制台中并且不是空的我不知道为什么它不执行 switch 语句是否有任何语法错误?是不是我创建了一个具有不同框架的项目?

标签: c#.netvisual-studiowinforms

解决方案


问题实际上是我的数据源,即返回尾随空格。

string role = myReader.GetString(3).ToString()

没有回来

a

'a               '

为了解决这个问题,我只是添加.TrimEnd()以摆脱尾随

而不是这个

string role = myReader.GetString(3).ToString()

我用这个:

string role = myReader.GetString(3).TrimEnd()

感谢所有帮助过的人!


推荐阅读