找到你要的答案

Q:C# login system

Q:C #登录系统

Currently I'm experimenting how I can make a login system, with different userinput. (Every user has his own information).I've two database tables, one is called "user" (here is id primary key, and username and password are in it) and the second table is the "userInformation" (here is id the foreign key and there is information like name,adres etc.) I've made it like this:

  public partial class standardUserInterface : Form
{
    int userIdNumber;
    string[] userinfo;
    public standardUserInterface(int userIdNumber)
    {
        InitializeComponent();
        userinfo = new string[3];
        this.userIdNumber = userIdNumber;
        string selectUserData = "select voornaam,achternaam,woonplaats from dbo.usersinfo inner join dbo.loginuser on usersinfo.id=@loginUserId";
        SqlConnection conn = sqlConn.openSqlConnection();
        conn.Open();
        SqlCommand comm = new SqlCommand(selectUserData, conn);
        comm.Parameters.AddWithValue("@loginUserId", userIdNumber);
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
            for (int i = 0; i < 3; i++)
            {
                userinfo[i] = dr.GetString(i);
            }
        }
        lblVoornaam.Text = userinfo[0];
        lblAchternaam.Text = userinfo[1];
        lblAdres.Text = userinfo[2];
    }
}

What do you think about this? Is there a better way, or is this done right?

EDIT

This is the login before it's receiving the userdata

  private string password;
    private string username;
    private int userIdNumber;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        username = txtUsername.Text;
        password = txtPassword.Text;
        userLogin user = new userLogin(username, password);
        //wachtwoord & gebruikersnaam controleren
        if (username != "" & password != "")
        {
            if (user.checkPassword())
            {
                this.Hide();
                userIdNumber = user.checkID();
                standardUserInterface openUserInterface = new standardUserInterface(userIdNumber);
                openUserInterface.ShowDialog();
            }
            else
            {
                MessageBox.Show("wachtwoord is onjuist");
            }
        }
        else
        {
            MessageBox.Show("vul alle velden in");
        }
    }

目前我正在尝试如何登录系统,不同的用户输入。(每一个用户都有自己的信息)。我两个数据库表,一个被称为“用户”(这里是id主键和用户名和密码在它)和第二个表“用户信息”(这里是ID外键有信息如姓名、地址等)我让它这样:

  public partial class standardUserInterface : Form
{
    int userIdNumber;
    string[] userinfo;
    public standardUserInterface(int userIdNumber)
    {
        InitializeComponent();
        userinfo = new string[3];
        this.userIdNumber = userIdNumber;
        string selectUserData = "select voornaam,achternaam,woonplaats from dbo.usersinfo inner join dbo.loginuser on usersinfo.id=@loginUserId";
        SqlConnection conn = sqlConn.openSqlConnection();
        conn.Open();
        SqlCommand comm = new SqlCommand(selectUserData, conn);
        comm.Parameters.AddWithValue("@loginUserId", userIdNumber);
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
            for (int i = 0; i < 3; i++)
            {
                userinfo[i] = dr.GetString(i);
            }
        }
        lblVoornaam.Text = userinfo[0];
        lblAchternaam.Text = userinfo[1];
        lblAdres.Text = userinfo[2];
    }
}

你觉得这个怎么样?有没有更好的办法,还是这样做对不对?

编辑

这是在它的接收用户登录

  private string password;
    private string username;
    private int userIdNumber;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        username = txtUsername.Text;
        password = txtPassword.Text;
        userLogin user = new userLogin(username, password);
        //wachtwoord & gebruikersnaam controleren
        if (username != "" & password != "")
        {
            if (user.checkPassword())
            {
                this.Hide();
                userIdNumber = user.checkID();
                standardUserInterface openUserInterface = new standardUserInterface(userIdNumber);
                openUserInterface.ShowDialog();
            }
            else
            {
                MessageBox.Show("wachtwoord is onjuist");
            }
        }
        else
        {
            MessageBox.Show("vul alle velden in");
        }
    }
answer1: 回答1:

Don't we need a authentication system for log in, wherein we can validate the used id and password? only if the validation is successful we will go for fetching the user details

one suggestion, it will be better if we can create a stored procedure for getting the user data instead of the inline query.

我们不需要登录的身份验证系统,我们可以验证所使用的ID和密码吗?只有当验证成功时,我们才会去获取用户的详细信息

一个建议,如果我们可以创建一个存储过程来获取用户数据而不是内联查询,这将是更好的。

answer2: 回答2:

I've done it now (the stored procedure)`

SqlCommand comm = new SqlCommand("searchForUserInformation", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@loginUserId", userIdNumber);`

我现在做了(存储过程)

SqlCommand comm = new SqlCommand("searchForUserInformation", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@loginUserId", userIdNumber);`
c#  mysql  login