c# winform实现简单的登录功能

来源:互联网 发布:淘宝怎么删差评 编辑:程序博客网 时间:2024/05/01 22:45

通过对用户名的校验,获取密码字段然后在和用户输入的md5加密后的密码进行比较

由于我的后续程序中使用了固定的日期格式yyyy-mm-dd所以我这里设置了电脑的短日期格式

数据库连接字符串,代码中我是直接从配置文件读取的
Data Source=ZJC-9;Initial Catalog=BILLINFO;User ID=sa;Password=123456" providerName="System.Data.SqlClient
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Configuration;using System.Data.SqlClient;using System.Security.Cryptography;using System.Runtime.InteropServices;namespace user{    public partial class login : Form    {        public login()        {            InitializeComponent();        }        public static string username;        public static string password;        //设置客户端电脑短日期格式为yyyy-mm-dd        [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]        public static extern int GetSystemDefaultLCID();        [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]        public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);        public const int LOCALE_SLONGDATE = 0x20;        public const int LOCALE_SSHORTDATE = 0x1F;        public const int LOCALE_STIME = 0x1003;        public void SetDateTimeFormat()        {            try            {                int x = GetSystemDefaultLCID();                SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd");   //短日期格式            }            catch (Exception ex)            {                messagebox.show(ex.tostring());            }        }        //登录按钮        private void button1_Click(object sender, EventArgs e)        {            string sqlconn = ConfigurationManager.ConnectionStrings["connstring"].ToString();//读配置文件取得连接字符串            SqlConnection conn = new SqlConnection(sqlconn);            SqlCommand cmd = new SqlCommand();            string pwd = getMD5(textBox2.Text);            cmd.CommandText="select * from dbo.or_userinfo where userid='"+textBox1.Text+"' and password='"+ pwd +"'";//sql查询语句            cmd.Connection = conn;            SqlDataReader myreader;            try            {                conn.Open();                myreader = cmd.ExecuteReader();                if(myreader.Read())                {                    username = textBox1.Text;                    password = textBox2.Text;                    SetDateTimeFormat();                    Form1 frm1 = new Form1();                    this.Hide();                    frm1.ShowDialog();                    this.Close();                }                else                {                    MessageBox.Show("登录失败,用户名或密码错误!","Error");                }            }            catch (Exception ex)            {                MessageBox.Show("连接数据库失败,请检查网络!"+ex.Message.ToString()+"","Message");            }            conn.Close();        }        //获取MD5校验值        public static String getMD5(String requestBody)        {            if (requestBody == null)                return null;            MD5 md5Hasher = MD5.Create();            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(requestBody));            StringBuilder sBuilder = new StringBuilder();            for (int i = 0; i < data.Length; i++)            {                sBuilder.Append(data[i].ToString("x2"));            }            return sBuilder.ToString();        }    }}

下面是登录窗体的贴图

0 0
原创粉丝点击