当前位置:数据库 > SQLServer >>

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

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

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

 


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

[csharp]
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Data.SqlClient; 
using System.Windows.Forms; 
using System.Data.OleDb; 
namespace 登录数据库 

    public partial class Form2 : Form 
    { 
        public Form2() 
        { 
            InitializeComponent(); 
        } 
        private void button1_Click(object sender, EventArgs e) 
        { 
            if (textBox1.Text == "" || textBox2.Text == "")  
            MessageBox.Show("提示:请输入用户名和密码!", "警告"); 
            SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True"); 
            conn.Open(); 
            SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim() + "' and 密码='" + textBox2.Text.Trim() + "'", conn); 
            SqlDataReader sdr = cmd.ExecuteReader(); 
            sdr.Read(); 
            if (sdr.HasRows) 
                MessageBox.Show("登录成功!", "提示"); 
            else 
            MessageBox.Show("提示:学生用户名或密码错误!","警告"); 
            conn.Close(); 
        } 
        private void button2_Click(object sender, EventArgs e) 
        { 
            if (textBox1.Text == "" || textBox2.Text == "") 
            MessageBox.Show("请输入用户名、密码!", "警告"); 
            else 
            { 
                SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True"); 
                conn.Open(); 
                SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim()+"'", conn); 
                SqlDataReader sdr = cmd.ExecuteReader(); 
                sdr.Read(); 
                if (sdr.HasRows) 
                    MessageBox.Show("该用户已注册,请使用其他用户名", "提示"); 
                else 
                { 
                        sdr.Close(); 
                        string myinsert = "insert into 用户(用户名,密码) values ('" + textBox1.Text + "','" + textBox2.Text + "')"; 
                        SqlCommand mycom = new SqlCommand(myinsert, conn);           //定义OleDbCommnad对象并连接数据库 
                        mycom.ExecuteNonQuery();                           //执行插入语句 
                        conn.Close();                //关闭对象并释放所占内存空间   
                        conn.Dispose(); 
                        MessageBox.Show("您已注册成功!"); 
                } 
            } 
        } 
    } 
}    
登录界面


注册界面

 

 

补充:软件开发 , Vc ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,