1

プログラムで管理者ユーザーを検出するのに問題があります。ログイン システムを作成しましたが、管理者がログインすると、SQL クエリをスキップして、管理者ではなくユーザー画面を開きます。ユーザーが登録すると、ユーザー タイプを選択するためのラジオ ボタンのペアが表示されます。どちらを選択したかに応じて、ユーザー タイプ (管理者またはユーザー) がデータベースのユーザー列に書き込まれます。これが私のコードです:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace myLoginProject
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes");
        connection.Open();
        string selection = "select * from Logins where Name = '" + userNameBox.Text + "' and Password = '" + passwordBox.Text + "' ";
        SqlCommand command = new SqlCommand(selection, connection);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
    }

    private void registerButton_Click(object sender, EventArgs e)
    {
        adminAuthScreen aas = new adminAuthScreen();
        aas.Show();
    }
    private int myMethod(string user, string pass)
    {
        user.Trim();
        pass.Trim();
        SqlConnection connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes");
        connection.Open();
        string selection = "select * from Logins where Name = '"+user+"' and Password = '"+pass+"' ";
        SqlCommand command = new SqlCommand(selection, connection);
        if (command.ExecuteScalar() != null)
            return 1;
        else
            return 0;

    }

    private void loginButton_Click(object sender, EventArgs e)
    {
        if (myMethod(userNameBox.Text,passwordBox.Text)>0)
        {
            MessageBox.Show("Welcome back, "+userNameBox.Text);
            SqlConnection myConnection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes");
            try
            {
                myConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           string checkAdmin1 = "SELECT * FROM Logins WHERE Name = '"+userNameBox.Text+"' AND User='Admin'";
        SqlCommand checkIfAdmin = new SqlCommand(checkAdmin1, myConnection);
        if (checkIfAdmin.ExecuteScalar() != null)
        {
           adminScreen admnscrn = new adminScreen();
            admnscrn.Show();
        }
        else
        {
            userScreen usrscrn = new userScreen();
            usrscrn.Show();
        }
        }
    }


    public SqlConnection connection { get; set; }
}
}

このビットが問題のようです(少なくともデバッグ中の問題です):

 private void loginButton_Click(object sender, EventArgs e)
        {
            if (myMethod(userNameBox.Text,passwordBox.Text)>0)
            {
                MessageBox.Show("Welcome back, "+userNameBox.Text);
                SqlConnection myConnection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes");
                try
                {
                    myConnection.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
               string checkAdmin1 = "SELECT * FROM Logins WHERE Name = '"+userNameBox.Text+"' AND User='Admin'";
            SqlCommand checkIfAdmin = new SqlCommand(checkAdmin1, myConnection);
            if (checkIfAdmin.ExecuteScalar() != null)
            {
               adminScreen admnscrn = new adminScreen();
                admnscrn.Show();
            }
            else
            {
                userScreen usrscrn = new userScreen();
                usrscrn.Show();
            }
            }
        }

問題が何であるかを知るのを手伝ってくれる人はいますか??? グーグルで調べてみましたが、どこにも答えが見つからないようです... PS C#で書かれたwinformsアプリで、1台のコンピューターで実行されます

4

0 に答える 0