专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > VSTS

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

发布时间:2011-06-20 18:19:38 文章来源:www.iduyao.cn 采编人员:星星草
VS连接SQL Server 2008,并实现登录和注册功能

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

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


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

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("您已注册成功!");
                }
            }
        }
    }
}	

登录界面


注册界面




2楼zhangmy2798昨天 23:00
WAWA厉害
Re: Cambridgeacm昨天 23:39
回复zhangmy2798呵呵,都是基础的,我才学
1楼Forxujiangbo昨天 17:33
最基础的
Re: Cambridgeacm昨天 18:00
回复Forxujiangbo呵呵,我是菜鸟,才开始学
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: