回 帖 发 新 帖 刷新版面

主题:如何用C#编写用户登陆界面

求助如何用C#编写用户登陆界面,最好举一个例子。

回复列表 (共1个回复)

沙发

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using LibraryManagementSystem.Global;

namespace LibraryManagementSystem.Forms
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
            CD.dataTable = new DataTable();
        }

        //----------------------------------------------------
        //private static SqlConnection connection = new SqlConnection(MyString.DatabaseConnection);
        //private SqlCommand command;
        //private SqlDataReader dataReader;
        
        //----------------------------------------------------

        private void btnReset_Click(object sender, EventArgs e)
        {
            tbUsername.Text = "";
            tbPsw.Text = "";
            tbUsername.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CD.connection.State == ConnectionState.Closed)
            {
                CD.connection.Open();
            }

            string selectionStr = "SELECT * FROM UserAccounts WHERE username ='" + tbUsername.Text.Trim() + "'";
            CD.command = new SqlCommand(selectionStr, CD.connection);
            CD.dataReader = CD.command.ExecuteReader();

            if (!CD.dataReader.Read())
            {
                MessageBox.Show("无效用户名!\n您输入的用户名是: " + tbUsername.Text, "无效用户名", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (tbUsername.Text.Trim() == CD.dataReader[0].ToString() && tbPsw.Text.Trim() == CD.dataReader[1].ToString())
                {
                    MessageBox.Show(tbUsername.Text + ", 您好, 这是你第 " + CD.dataReader[4].ToString() + " 次登陆.\n您上次登陆时间是: " + CD.dataReader[3].ToString(), "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    MyString.UserName = CD.dataReader[0].ToString();
                    string access = CD.dataReader[2].ToString();
                    CD.dataReader.Close();
                    CD.command = new SqlCommand("UPDATE UserAccounts SET lastlogintime='" + DateTime.Now.ToString() + "'", CD.connection);
                    CD.command.ExecuteNonQuery();
                    CD.command = new SqlCommand("UPDATE UserAccounts SET logintimes=logintimes+1", CD.connection);
                    CD.command.ExecuteNonQuery();
                    FormMain f = new FormMain(MyString.UserName, access);
                    f.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("对不起, 您输入的用户名或密码有误!\n您输入的用户名是: " + tbUsername.Text, "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tbUsername.Focus();
                    CD.dataReader.Close();
                }
            }
        }
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册