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

首先创建数据库:

[Vip]

创建三张表:

分别是:

[VipInformation](会员信息)

 

[Log](日志)

 

[VipAccount](账户权限)

详细语句:

 

 1 --创建数据库[Vip]
 2 create database Vip
 3 on primary
 4 (
 5     name='Vip'
 6     ,filename='E:\vip\Vip.mdf'
 7     ,size=3MB
 8     ,maxsize=100MB
 9     ,filegrowth=10%
10 )
11 log on
12 (
13     name='Vip_log'
14     ,filename='E:\Vip\Vip_log.ldf'
15     ,size=1MB
16     ,maxsize=unlimited
17     ,filegrowth=1MB
18 )
19 Go
20 --查询数据库[Vip]
21 use vip
22 --创建表[VipInformation](会员信息)
23 create table VipInformation
24 (
25     vId int identity(1,1) primary key
26     ,vName nvarchar(30)
27     ,vGender nchar(2)
28     ,vAge int
29     ,vAddress nvarchar(50)
30     ,vPhone varchar(20)
31 )
32 --查询表[VipInformation]
33 select * from VipInformation
34 --创建表[Log](日志)
35 create table [Log]
36 (
37     id int identity(1,1) primary key
38     ,vName nvarchar(30)
39     ,situation nchar(10)
40     ,[time] datetime
41     ,vType nvarchar(50)
42 )
43 --查询表[Log]
44 select * from [log]
45 --创建表[VipAccount](账户权限)
46 create table VipAccount
47 (
48     vId int identity(1,1) primary key
49     ,vUserName nvarchar(20) not null
50     ,vUserPwd nchar(20) not null
51     ,UserType nvarchar(50) null
52 )
53 --查询表[VipAccount]
54 select * from VipAccount
55 --插入表[VipAccount]
56 insert into VipAccount(vUserName,vUserPwd,UserType) values('admin','123','Administrator')

 

 配置App.config文件

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

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

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

 

登录界面:

详细代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Configuration;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 using System.Data.SqlClient;
12 
13 namespace 会员管理系统
14 {
15     public partial class VIPLogin : Form
16     {
17         public VIPLogin()
18         {
19             InitializeComponent();
20         }
21         //用于连接配置文件App.config
22         string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
23         //登录按钮
24         private void btnLogin_Click(object sender, EventArgs e)
25         {
26             //连接数据库语句
27             using(SqlConnection con=new SqlConnection(connStr))
28             {
29                 //操作数据库语句
30                 string sql = "select vuserpwd from vipaccount where vUserName='" + txtName.Text + "'";
31                 using(SqlCommand cmd=new SqlCommand(sql,con))
32                 {
33                     //打开数据库
34                     con.Open();
35                     //使用 SqlDataReader 来 读取数据库
36                     using (SqlDataReader sdr = cmd.ExecuteReader())
37                     {
38                         //SqlDataReader 在数据库中为 从第1条数据开始 一条一条往下读
39                         if (sdr.Read()) //如果读取账户成功(文本框中的用户名在数据库中存在)
40                         {
41                             //则将第1条 密码 赋给 字符串pwd  ,并且依次往后读取 所有的密码
42                             //Trim()方法为移除字符串前后的空白
43                             string pwd = sdr.GetString(0).Trim();
44                             //如果 文本框中输入的密码 ==数据库中的密码
45                             if (pwd == txtPwd.Text)
46                             {
47                                 //说明在该账户下 密码正确, 系统登录成功
48                                 MessageBox.Show("登录成功,正在进入主界面......");
49 
50                             }
51                             else
52                             {
53                                 //密码错误
54                                 MessageBox.Show("密码错误,请重新输入");
55                                 txtPwd.Text = "";
56                             }
57                         }
58                         else
59                         {
60                             //用户名错误
61                             MessageBox.Show("用户名错误,请重新输入!");
62                             txtName.Text = "";
63                         }
64                     }
65                 }
66             }
67         }
68 
69     }
70 }

 

待续。。。

 

posted @ 2016-04-20 16:13  翻滚吧炒鸡蛋  阅读(7042)  评论(0编辑  收藏  举报