gpt4 book ai didi

c# - 根据登录条件停止?

转载 作者:行者123 更新时间:2023-11-30 16:19:46 25 4
gpt4 key购买 nike

我是 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();
}

最佳答案

您正在调用 ShowDialog 两次。试试这个:

Loginf fLogin = new Loginf();
DialogResult result = fLogin.ShowDialog();
if (result == DialogResult.OK)
{
Application.Run(new Home2());
}
else if (result == DialogResult.No)
{
Application.Run(new Home3());
}
else
{
Application.Exit();
}

调用 ShowDialog 两次将显示窗体两次。使用它只会显示一次。在您的代码中,如果角色不是“admin”,则执行 else block ,并调用 ShowDialog again 将显示 again,这不是你想要的。显示一次表单,存储结果,并对存储的结果进行检查。

关于c# - 根据登录条件停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com