注册 登录

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Data;
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 button1_Click(object sender, EventArgs e)
        {
            //读取用户名密码
            string name = textBox1.Text;
            string upass = textBox2.Text;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database='进销存';user=sa;pwd=123456");
            conn.Open();
            //写执行语句
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from users where name like @name and upass like @upass";
            cmd.Parameters.Add("@name",name);
            cmd.Parameters.Add("upass", upass);
            //执行sql语句返回查询内容
            SqlDataReader dr = cmd.ExecuteReader();
            //读取dr
            if (dr.Read())
            {
                采购入库单 m = new 采购入库单();
                this.Owner = m;
                this.Visible = false;
                m.Show();
            }
            else
            {
                MessageBox.Show("登陆失败!");
            }
            conn.Close();

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //添加数据到数据库
            string username = textBox1.Text;
            string upass = textBox2.Text;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database='进销存';user=sa;pwd=123456");
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into users values(' " + username + "','" + upass + "')";
            //增删改
            int count= cmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("注册成功!");
            }
            else
            {
                MessageBox.Show("注册失败!");
            }
        }
原文地址:https://www.cnblogs.com/zxm1002/p/4939721.html