设为首页 加入收藏

TOP

VS连接SQL Server 2008,并实现登录和注册功能
2014-11-23 19:22:51 】 浏览:1787
Tags:连接 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改变DialogBar的背景色 下一篇VC密码框显示字符的终极设置方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目