吉吉于

free

C#每天抄一点(36):带数据库的登陆系统


Mainform.cs

01 /*
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/10/31
05 * 时间: 21:05
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Collections.Generic;
11 using System.ComponentModel;
12 using System.Drawing;
13 using System.Linq;
14 using System.Windows.Forms;
15 using System.Data;
16 using System.Data.OleDb;
17 namespace Lazy36_带有数据库的登陆系统
18 {
19
20     public partial class MainForm : Form
21     {
22         public MainForm()
23         {
24
25             InitializeComponent();
26
27
28         }
29
30         void Button1Click(object sender, EventArgs e)
31         {
32             if(textBox1.Text==“”|| textBox2.Text==“”)
33             {
34                 MessageBox.Show(“输入登录信息不完整,请重新输入~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
35             }
36             else
37             {
38                 string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
39                 //连接数据库的路径及数据库名
40                 string Lazy_constr=“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =”+Lazy_path;
41                  //生成连接数据库字符串
42                 OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
43                 //定义OleDbConnection对象实例并连接数据库
44                 Lazy_connect.Open();
45                 string Lazy_sql=“select * from Person where 账号=’”+textBox1.Text+“‘and 密码=’”+textBox2.Text+“‘”;
46                 OleDbDataAdapter Lazy_data=new OleDbDataAdapter(Lazy_sql,Lazy_connect);
47                 DataTable Lazy_table=new DataTable();
48                 Lazy_data.Fill(Lazy_table);
49                 int i=Lazy_table.Rows.Count;
50                 if(i>=1)
51                 {
52                     Form1 Lazy_form=new Form1();
53                     Lazy_form.Show();
54                     Lazy_form.label1.Text=“欢迎”+ textBox1.Text+“登录!”;
55                 }
56                 else
57                 {
58                     MessageBox.Show(“用户名或密码不正确,请重新输入~”,“提示”,MessageBoxButtons.OK,MessageBoxIcon.Information);
59                 }
60             }
61         }
62
63         void Button2Click(object sender, EventArgs e)
64         {
65             this.textBox1.Text=“”;
66             this.textBox2.Text=“”;
67         }
68
69         void Button3Click(object sender, EventArgs e)
70         {
71             Application.Exit();
72         }
73
74
75     }
76 }

Form1.cs

01 /*
02 * 由SharpDevelop创建。
03 * 用户: Lazynight
04 * 日期: 2011/10/31
05 * 时间: 21:05
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Drawing;
11 using System.Windows.Forms;
12 using System.Data;
13 using System.Data.OleDb;
14 namespace Lazy36_带有数据库的登陆系统
15 {
16
17     public partial class Form1 : Form
18     {
19         public Form1()
20         {
21
22             InitializeComponent();
23
24         }
25
26         void Form1Load(object sender, EventArgs e)
27         {
28             string Lazy_path=Application.StartupPath+“\\Lazy_Data.accdb”;
29             //连接数据库的路径及数据库名
30             string Lazy_constr=“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=”+Lazy_path;
31              //生成连接数据库字符串
32             OleDbConnection Lazy_connect=new OleDbConnection(Lazy_constr);
33             //定义OleDbConnection对象实例并连接数据库
34             string Lazy_sql=“select * from LazyData”;
35             //Sql查询语句
36             OleDbDataAdapter Lazy_data=new OleDbDataAdapter(Lazy_sql,Lazy_connect);
37             //定义OleDbDataAdapter对象实例并连接数据库表
38             DataSet Lazy_set=new DataSet();
39             //定义DataSet对象实例 
40             Lazy_data.Fill(Lazy_set);
41             //填充数据集
42             dataGridView1.DataSource= Lazy_set.Tables[].DefaultView;
43             //连接数据表格,显示数据
44             Lazy_connect.Close();
45             //关闭OleDbConnection对象实例
46             Lazy_connect.Dispose();
47             //释放OleDbConnection对象实例所占资源空间
48         }
49     }
50 }

下载源码

转载请注明:于哲的博客 » C#每天抄一点(36):带数据库的登陆系统