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 }

 

待續。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM