我是 C# 新手,所以我的问题出在登录表单上。
如果每次我点击提交按钮时我的用户类不是“admin”,它就会让我回到登录表单。所以当我猜的条件不正确时,我的陈述就会停止。这是我的代码。
--------编辑对不起,我的新手限制是我所拥有的:一个带有用户名和角色的 sql 表,具体取决于他们拥有的角色,用户将加载不同的表单
// Compare strings
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
// button on Login form
public void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection UGIcon = new SqlConnection();
UGIcon.ConnectionString = "Data Source=BVSQL; Initial Catalog=BV1;user id=jose; password=jones6;";
UGIcon.Open();
SqlCommand cmd = new SqlCommand("SELECT ISNULL(bvuser, '') AS stUsername, ISNULL(bvpassword,'') AS stPassword, ISNULL(bvclass, '') AS stRole FROM BVusertable WHERE bvuser='" + textBox1.Text + "' and bvpassword='" + textBox2.Text + "'", UGIcon);
SqlDataReader dr = cmd.ExecuteReader();
string userText = textBox1.Text;
string passText = textBox2.Text;
//string stRole = "admin";
dr.Read();
{
if
(this.CompareStrings(dr["stUsername"].ToString(), userText) &&
this.CompareStrings(dr["stPassword"].ToString(), passText)
)
{
if (this.CompareStrings(dr["stRole"].ToString(), "admin"))
{
this.DialogResult = DialogResult.OK;
}
else if (this.CompareStrings(dr["stRole"].ToString(), "user"))
{
this.DialogResult = DialogResult.No;
}
}
else
{
//MessageBox.Show("Error");
}
}
dr.Close();
UGIcon.Close();
}
catch (Exception ex)
{
MessageBox.Show("Login Falied");
}
}
这是 Programs.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BV_SOFT
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Loginf fLogin = new Loginf();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new Home2());
}
else
if (fLogin.ShowDialog() == DialogResult.No)
{
Application.Run(new Home3());
}
else
{
Application.Exit();
}