关闭

Microsoft Visual Studio (c#)连接到数据库---Userlogin窗口--向先先

591人阅读 评论(0) 收藏 举报

登录窗口代码

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

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

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            string username = textusername.Text.Trim();
            string password = textpassword.Text;
            string cs = @"server=4JF200\SQL2008;database=Users;uid=sa;pwd=123456";
            SqlConnection con = new SqlConnection(cs);
            con.Open();
            string sql = string.Format(
                "select * from tb_user where username='{0}'",
                username);
            SqlCommand command = new SqlCommand(sql, con);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                if (reader["userpassword"].ToString() == password)
                {
                    MessageBox.Show("登录成功");
                }
                else
                {
                    MessageBox.Show("密码错误");
                }
            }
            else
            {
                MessageBox.Show("无效的用户名");
            }
            con.Close();
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnregister_Click(object sender, EventArgs e)
        {
            new Form2().ShowDialog();
        }
    }
}

注册窗口代码

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
             string username = textusername.Text.Trim();
            string password = textpassword.Text;
            string confirm = textConfirmpassword.Text;
            string email = textemail.Text.Trim();
            if (password == confirm) {
                string cs = @"server=4JF200\SQL2008;database=Users;uid=sa;pwd=123456";
                SqlConnection con = new SqlConnection(cs);
                try {
                    con.Open();
                    string sql = string.Format(
                        "insert into tb_user values ('{0}', '{1}', '{2}')",
                        username, password, email);
                    SqlCommand command = new SqlCommand(sql, con);
                    if (command.ExecuteNonQuery() == 1) {
                        MessageBox.Show("注册成功, 请登录",
                            "温馨提示", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                        this.Close();
                    }
                }
                catch (Exception) {
                    MessageBox.Show("注册失败, 请尝试更换用户名",
                        "错误", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
                finally {   // 总是执行代码
                    con.Close();
                }
            }
            else {
                MessageBox.Show("两次输入的密码不一致");
            }
        }
    }
}

--main--

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18854次
    • 积分:404
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:14篇
    • 译文:0篇
    • 评论:1条
    文章分类