C#_会员管理系统:开发一(用户登录)

时间:2023-03-10 06:14:00
C#_会员管理系统:开发一(用户登录)

首先创建数据库:

[Vip]

创建三张表:

分别是:

[VipInformation](会员信息)

[Log](日志)

[VipAccount](账户权限)

详细语句:

 --创建数据库[Vip]
create database Vip
on primary
(
name='Vip'
,filename='E:\vip\Vip.mdf'
,size=3MB
,maxsize=100MB
,filegrowth=10%
)
log on
(
name='Vip_log'
,filename='E:\Vip\Vip_log.ldf'
,size=1MB
,maxsize=unlimited
,filegrowth=1MB
)
Go
--查询数据库[Vip]
use vip
--创建表[VipInformation](会员信息)
create table VipInformation
(
vId int identity(1,1) primary key
,vName nvarchar(30)
,vGender nchar(2)
,vAge int
,vAddress nvarchar(50)
,vPhone varchar(20)
)
--查询表[VipInformation]
select * from VipInformation
--创建表[Log](日志)
create table [Log]
(
id int identity(1,1) primary key
,vName nvarchar(30)
,situation nchar(10)
,[time] datetime
,vType nvarchar(50)
)
--查询表[Log]
select * from [log]
--创建表[VipAccount](账户权限)
create table VipAccount
(
vId int identity(1,1) primary key
,vUserName nvarchar(20) not null
,vUserPwd nchar(20) not null
,UserType nvarchar(50) null
)
--查询表[VipAccount]
select * from VipAccount
--插入表[VipAccount]
insert into VipAccount(vUserName,vUserPwd,UserType) values('admin','','Administrator')

配置App.config文件

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
//连接数据库字符串
<add name="str" connectionString="Data Source=QH-20160401XDTT;Initial Catalog=Vip;Integrated Security=True"/>
</connectionStrings>
</configuration>

设置完成 App.config 连接配置文件后,我们需要在登录窗体代码中来对其进行连接;这里我们需要用到ConfigurationManager(它提供了对客户端应用程序配置文件的访问).系统默认是不使用其命名空间的,因此我们需要对其进行解析;在解析前,需要添加一个对System.configuration程序集的引用.

C#_会员管理系统:开发一(用户登录)

程序集-框架, 选择System.configuration,然后点确定.

C#_会员管理系统:开发一(用户登录)

登录界面:

C#_会员管理系统:开发一(用户登录)

详细代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; namespace 会员管理系统
{
public partial class VIPLogin : Form
{
public VIPLogin()
{
InitializeComponent();
}
//用于连接配置文件App.config
string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
//登录按钮
private void btnLogin_Click(object sender, EventArgs e)
{
//连接数据库语句
using(SqlConnection con=new SqlConnection(connStr))
{
//操作数据库语句
string sql = "select vuserpwd from vipaccount where vUserName='" + txtName.Text + "'";
using(SqlCommand cmd=new SqlCommand(sql,con))
{
//打开数据库
con.Open();
//使用 SqlDataReader 来 读取数据库
using (SqlDataReader sdr = cmd.ExecuteReader())
{
//SqlDataReader 在数据库中为 从第1条数据开始 一条一条往下读
if (sdr.Read()) //如果读取账户成功(文本框中的用户名在数据库中存在)
{
//则将第1条 密码 赋给 字符串pwd ,并且依次往后读取 所有的密码
//Trim()方法为移除字符串前后的空白
string pwd = sdr.GetString().Trim();
//如果 文本框中输入的密码 ==数据库中的密码
if (pwd == txtPwd.Text)
{
//说明在该账户下 密码正确, 系统登录成功
MessageBox.Show("登录成功,正在进入主界面......"); }
else
{
//密码错误
MessageBox.Show("密码错误,请重新输入");
txtPwd.Text = "";
}
}
else
{
//用户名错误
MessageBox.Show("用户名错误,请重新输入!");
txtName.Text = "";
}
}
}
}
} }
}

待续。。。