首页 > 代码库 > ASP.NET用户登录按钮事件

ASP.NET用户登录按钮事件

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace HotelMIS
 7 {
 8     public static class DBHelper
 9     {
10         //连接数据库字符串
11         public static readonly string conStr = "Server=.;Database=HotelManager;Integrated security=true";
12     }
13 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using Hotel.BLL;
10 using System.Data.SqlClient;
11 
12 namespace HotelMIS
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         //登录按钮事件
22         private void btnLogin_Click(object sender, EventArgs e)
23         {
24             if (textBox1.Text.Trim() == "" || string.IsNullOrEmpty(textBox1.Text))
25             {
26                 MessageBox.Show("用户名不能为空!", "登录提示");
27             }
28             else if (textBox2.Text.Trim() == "" || string.IsNullOrEmpty(textBox2.Text))
29             {
30                 MessageBox.Show("密码不能为空", "登录提示");
31             }
32             else
33             {
34                 SqlConnection con = new SqlConnection(DBHelper.conStr);
35                 try
36                 {
37                     con.Open();
38                     string sql = string.Format("select * from HotelUser where UserName = ‘{0}‘ and Password = ‘{1}‘", textBox1.Text, textBox2.Text);
39                     SqlCommand cmd = new SqlCommand(sql, con);
40                     SqlDataReader dr = cmd.ExecuteReader();
41                     if (dr.Read())
42                     {
43                         MessageBox.Show("登录成功", "登录提示");
44                     }
45                     else
46                     {
47                         MessageBox.Show("用户名或密码错误", "登录提示");
48                     }
49                     dr.Close();
50                 }
51                 catch (Exception)
52                 {
53                     MessageBox.Show("请检查连接字符串", "登录提示");
54                 }
55                 finally
56                 {
57                     con.Close();
58                 }
59             }
60         }
61     }
62 }