C# 连接数据库(MD5加密存储)演示注册与登录

来源:互联网 发布:淘宝宠物药店 编辑:程序博客网 时间:2024/06/01 23:40

工具:Visual Studio 2008

           SQL Server 2008

           Windows XP

 

Form1:

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.Data.SqlClient;using System.Security.Cryptography;namespace loginVs{    public partial class Form1 : Form    {        public bool b1 = false;        public Form1()        {            InitializeComponent();                     }        private string MD5Create(string STR) //STR为待加密的string        {            string pwd = "";            //pwd为加密结果            MD5 md5 = MD5.Create();            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(STR));            //这里的UTF8是编码方式,你可以采用你喜欢的方式进行,比如UNcode等等            for (int i = 0; i < s.Length; i++)            {                pwd = pwd + s[i].ToString();            }            return pwd;        }        private void Form1_Load(object sender, EventArgs e)        {                           }        private void button2_Click(object sender, EventArgs e)        {            if (names.Text.Trim() == "")            {                MessageBox.Show("用户名不能为空!", "提示信息");                names.Focus();                return;            }            if (pwd.Text.Trim() == "")            {                MessageBox.Show("密码不能为空!", "提示信息");                pwd.Focus();                return;            }            int i = 0;            string connextionString = "server=localhost;database=TestSQL;uid=sa;pwd=huxiao1225";            SqlConnection mySqlConnection = new SqlConnection(connextionString);            using (mySqlConnection)            {                mySqlConnection.Open();                string username;                string pwdx;                username = MD5Create(names.Text.Trim());                pwdx= MD5Create(pwd.Text.Trim());                //用户名与密码经过MD5加密后于数据库表进行比对                string sql = "select count(*) from tb where names='" + username + "' and pwd='" + pwdx + "'";                using (SqlCommand cmd = mySqlConnection.CreateCommand())                {                    cmd.CommandText = sql;                    i = (int)cmd.ExecuteScalar();                }            }            if (i > 0)            {                b1 = true;                MessageBox.Show("登陆成功", "提示信息");                //MDIParent1.TxtUser = names.Text.Trim();                this.Close();            }            else            {                MessageBox.Show("用户名或密码错误!请重新登陆!", "提示信息");            }        }        private void button1_Click(object sender, EventArgs e)        {            LoginForm LoginForm = new LoginForm();            LoginForm.ShowDialog();        }    }}


 

 

 Form2:

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.Data.SqlClient;using System.Security.Cryptography;namespace loginVs{    public partial class LoginForm : Form    {          public LoginForm()        {                        InitializeComponent();            label4.Visible = false;            label5.Visible = false;            label6.Visible = false;            label7.Visible = false;            label8.Visible = false;        }        private  string MD5Create(string STR) //STR为待加密的string        {            string pwd = "";            //pwd为加密结果            MD5 md5 = MD5.Create();            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(STR));            //这里的UTF8是编码方式,你可以采用你喜欢的方式进行,比如UNcode等等            for (int i = 0; i < s.Length; i++)            {                pwd = pwd + s[i].ToString();            }            return pwd;        }        private void btnLogin_Click(object sender, EventArgs e)        {            if (tbxUsername.Text==string.Empty)            {                label4.Visible = true;                tbxUsername.Focus();                return;            }            if (tbxPwd.Text==string.Empty)            {                label5.Visible = true;                tbxPwd.Focus();                return;            }            if (tbxPwdSure.Text==string.Empty)            {                label6.Visible = true;                tbxPwdSure.Focus();                return;            }            if (tbxPwd.Text.Trim()!=tbxPwdSure.Text.Trim())            {                label7.Visible = true;                tbxPwdSure.Focus();                tbxPwdSure.Text = string.Empty;                return;            }            SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=TestSQL;uid=sa;pwd=huxiao1225");            using(mySqlConnection)            {                if (mySqlConnection.State==ConnectionState.Closed)                {                    mySqlConnection.Open();                }                SqlCommand cmd = new SqlCommand();                cmd.Connection = mySqlConnection;                string username;                string pwd;                username = MD5Create(tbxUsername.Text.Trim());                pwd = MD5Create(tbxPwd.Text.Trim());                string sql1 = "INSERT INTO tb(names,pwd) VALUES('" + username+ "','" + pwd + "')";//加密后添加到数据库中                cmd.CommandText = sql1;                if (cmd.ExecuteNonQuery() != -1)                {                    label8.Visible = true;                    timer1.Enabled = true;                }                else if (cmd.ExecuteNonQuery() == -1)                {                    MessageBox.Show("数据输入失败");                }                             }        }        private void timer1_Tick(object sender, EventArgs e)        {            timer1.Enabled = false;            this.Close();        }    }}


 

原创粉丝点击