登录

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string user = textBox1.Text;
            string pass = textBox2.Text;
            bool isok=checkuser(user,pass);
            if (isok)
            {
                MessageBox.Show("登录成功");
            }
            else
            {
                MessageBox.Show("登录失败");
            }
        }
        public bool checkuser(string user,string pass)
        {
            bool isok = false;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database=订购管理;user=sa;pwd=123");
            conn.Open();//打开数据库
            SqlCommand cmd = conn.CreateCommand();
            //执行语句
            cmd.CommandText = "select *from users where username='"+user+"' and  pass='"+pass+"'";
            //弹出个窗口看看语句格式是不是正确
            MessageBox.Show(cmd.CommandText);
            //遍历每一行数据,返回bool值
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                isok = true; 
            }
            conn.Close();
            return isok;
        }
    }
}
原文地址:https://www.cnblogs.com/happinesshappy/p/4542649.html