实验2 登录系统


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 LoginDemo
	{
	    public partial class Form1 : Form
	    {
	        public Form1()
	        {
	            InitializeComponent();
	        }
	
	        private void label2_Click(object sender, EventArgs e)
	        {
	
	        }
	
	        private void textBox2_TextChanged(object sender, EventArgs e)
	        {
	
	        }
	
	        private void button1_Click(object sender, EventArgs e)
	        {
	            string username = textBoxUserName.Text.Trim();  
	            string password = textBoxPassWord.Text.Trim();  
	
	            //string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
	            string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=luoluoluo";
	
	            SqlConnection sqlConnection = new SqlConnection(myConnString); 
	            sqlConnection.Open();
	
	            string sql = "select userid,password from usertable where userid = '" + username + "' and password = '" + password + "'";                                          
	            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
	
	            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
	
	            if (sqlDataReader.HasRows)
	            {
	                MessageBox.Show("SUCESS!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);       
	                label1.Text = "Log in :" + username;
	                
	                FormMain formMain = new FormMain(); 
	                formMain.Show();
	                this.Hide();  
	            }
	            else
	            {
	                MessageBox.Show("FAILED!", "notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
	            }
	            sqlConnection.Close();
	
	        }
	
	        private void textBox1_TextChanged(object sender, EventArgs e)
	        {
	
	        }
	
	        private void button2_Click(object sender, EventArgs e)
	        {
	            Application.Exit();
	        }
	
	        private void Form1_Load(object sender, EventArgs e)
	        {
	
	        }
	    }
	}
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Linq;
	using System.Text;
	using System.Threading.Tasks;
	using System.Windows.Forms;
	
	namespace LoginDemo
	{
	    public partial class FormMain : Form
	    {
	        public FormMain()
	        {
	            InitializeComponent();
	        }
	
	        private void FormMain_Load(object sender, EventArgs e)
	        {

	            this.usertableTableAdapter.Fill(this.testDataSet.usertable);
	
	        }
	
	        private void button1_Click(object sender, EventArgs e)
	        {
	            Application.Exit();
	        }
	    }
	}

原文地址:https://www.cnblogs.com/hzcya1995/p/13285163.html