注册与登陆基本功能 code从政1230005

282 阅读1分钟

登陆窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            register register = new register();
            register.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string qq = textBox1.Text;//账号
            string password = textBox2.Text;//密码
            string connStr = "Data Source=.;Initial Catalog=User;Integrated Security=True";//连接字符串
            SqlConnection conn = new SqlConnection(connStr);//连接数据库
            conn.Open();//打开数据库
            if (conn.State == ConnectionState.Open)
            {
                string sql = "select *from User1 where ID='" + qq + "' and Password='" + password + "'";//sql语句
                SqlCommand command = new SqlCommand(sql, conn);//执行sql命令
                SqlDataReader dataReader = command.ExecuteReader();//DataRead的数据都会往后移,直到数据为空;
                if (dataReader.HasRows)//HasRows:返回true或者false,表示从数据库中读取出来的数据集DataRead是否存在,用来判断是否为空;
                {
                    MessageBox.Show("登录成功!");
                }
                else
                {
                    textBox1.Text="";//账号
                    textBox2.Text="";//密码
                    MessageBox.Show("登录失败,账号或密码错误!");
                }
            }
            conn.Close();//关闭
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

注册窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class register : Form
    {
        public register()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string qq = textBox1.Text;//账号
            string password = textBox2.Text;//密码
            string connStr = "Data Source=.;Initial Catalog=User;Integrated Security=True";//连接字符串
            SqlConnection conn = new SqlConnection(connStr);//连接数据库
            conn.Open();//打开数据库
            if (conn.State == ConnectionState.Open)
            {
                string sql = "insert into User1 values ('" + qq + "','" + password + "')";
                SqlCommand command = new SqlCommand(sql, conn);
                int num = command.ExecuteNonQuery();//返回值
                if (num > 0)
                {
                    MessageBox.Show("新增成功");
                }
                else
                {
                    MessageBox.Show("新增失败");
                }
                conn.Close();
            }
        }

        private void register_Load(object sender, EventArgs e)
        {

        }
    }
}