VS连接SQL Server 2008,并实现登录和注册功能

来源:互联网 发布:父母之爱 知乎 编辑:程序博客网 时间:2024/06/10 22:07

VS连接SQL Server 2008,并实现登录和注册功能

建一个Student数据库,其中含有两张表,一个是用户表,其中包含能够登录该数据库的用户名和密码,还有一个是信息表,含有学生的信息


在VS中建一个Windows窗体应用程序,实现用户的登录和注册功能,登录时检索用户名和密码与用户表中的内容是否匹配,若匹配成功提示成功登录,否则登录失败,注册时检索用户名和密码和用户表中的内容是否有相同,若果有相同的提示该用户名已被注册,否则注册成功

[csharp] view plain copy
  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.Data.SqlClient;  
  9. using System.Windows.Forms;  
  10. using System.Data.OleDb;  
  11. namespace 登录数据库  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             if (textBox1.Text == "" || textBox2.Text == "")   
  22.             MessageBox.Show("提示:请输入用户名和密码!""警告");  
  23.             SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");  
  24.             conn.Open();  
  25.             SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim() + "' and 密码='" + textBox2.Text.Trim() + "'", conn);  
  26.             SqlDataReader sdr = cmd.ExecuteReader();  
  27.             sdr.Read();  
  28.             if (sdr.HasRows)  
  29.                 MessageBox.Show("登录成功!""提示");  
  30.             else  
  31.             MessageBox.Show("提示:学生用户名或密码错误!","警告");  
  32.             conn.Close();  
  33.         }  
  34.         private void button2_Click(object sender, EventArgs e)  
  35.         {  
  36.             if (textBox1.Text == "" || textBox2.Text == "")  
  37.             MessageBox.Show("请输入用户名、密码!""警告");  
  38.             else  
  39.             {  
  40.                 SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");  
  41.                 conn.Open();  
  42.                 SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim()+"'", conn);  
  43.                 SqlDataReader sdr = cmd.ExecuteReader();  
  44.                 sdr.Read();  
  45.                 if (sdr.HasRows)  
  46.                     MessageBox.Show("该用户已注册,请使用其他用户名""提示");  
  47.                 else  
  48.                 {  
  49.                         sdr.Close();  
  50.                         string myinsert = "insert into 用户(用户名,密码) values ('" + textBox1.Text + "','" + textBox2.Text + "')";  
  51.                         SqlCommand mycom = new SqlCommand(myinsert, conn);           //定义OleDbCommnad对象并连接数据库  
  52.                         mycom.ExecuteNonQuery();                           //执行插入语句  
  53.                         conn.Close();                //关闭对象并释放所占内存空间    
  54.                         conn.Dispose();  
  55.                         MessageBox.Show("您已注册成功!");  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60. }     

登录界面


注册界面


0 0