一、form1的设计
对应的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
string g = textBox2.Text;
string c = "";
string ql;
ql = "select zhanghao,mima,shenfen from b1 where zhanghao='" + a + "'and mima='" + g + "'";
DataSet ds = new DataSet();
ds = ClassLibrary1.DataAccess.GetData(ql);
if (ds.Tables[0].Rows.Count > 0)
{
c = ds.Tables[0].Rows[0][2].ToString();
//传递身份
ClassLibrary1.DataAccess.a = c;
ClassLibrary1.DataAccess.b = ds.Tables[0].Rows[0][0].ToString();//zhanghao
ClassLibrary1.DataAccess.c = ds.Tables[0].Rows[0][1].ToString();
if (c != null)
{
Form2 fm2 = new Form2();
fm2.Show();
this.Hide();
}
}
else { MessageBox.Show("密码或编号错误"); }
}
}
}
二、form2的设计
对应的主要控件是:menustrip、treeview、richtextbox 。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
DataSet aa = new DataSet();
private void Form2_Load(object sender, EventArgs e)
{
string a = ClassLibrary1.DataAccess.a;
if (a == "游客")
{
日记管理ToolStripMenuItem.Visible = false;
人员管理ToolStripMenuItem.Visible = false;
}
string ql = "select *from b2";
aa = ClassLibrary1.DataAccess.GetData(ql);
int i = aa.Tables[0].Rows.Count;
if (i > 0)
{
treeView1.Nodes.Clear();
for (int n = 0; n < i; n++)
{
TreeNode mytreenode = new TreeNode(aa.Tables[0].Rows[n][0].ToString());
treeView1.Nodes.Add(mytreenode);
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
int i = treeView1.SelectedNode.Index;
textBox1.Text = aa.Tables[0].Rows[i][0].ToString();
richTextBox1.Text = aa.Tables[0].Rows[i][1].ToString();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
richTextBox1.Text = "";
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || richTextBox1.Text == "")
{ MessageBox.Show("内容或主题不能为空"); }
else
{
int b = 0;
string ql = "insert into b2 values('" + textBox1.Text + "','" + richTextBox1.Text + "')";
for (int f = 0; f < aa.Tables[0].Rows.Count; f++)
{
if (textBox1.Text == aa.Tables[0].Rows[f][0].ToString()) b++;
}
if (b == 1) { MessageBox.Show("该主题已经存在"); }
else
{
ClassLibrary1.DataAccess.Update(ql);
MessageBox.Show("保存成功");
ql = "select *from b2";
aa = ClassLibrary1.DataAccess.GetData(ql);
int i = aa.Tables[0].Rows.Count;
if (i > 0)
{
treeView1.Nodes.Clear();
for (int n = 0; n < i; n++)
{
TreeNode mytreenode = new TreeNode(aa.Tables[0].Rows[n][0].ToString());
treeView1.Nodes.Add(mytreenode);
}
}
}
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//int i = treeView1.SelectedNode.Index;
string biaoti = treeView1.SelectedNode.Text;
string ql = "delete from b2 where biaoti='" + biaoti + "'";
DialogResult dr = MessageBox.Show("你真的想删除?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
ClassLibrary1.DataAccess.Update(ql);
ql = "select *from b2";
MessageBox.Show("ok");
aa = ClassLibrary1.DataAccess.GetData(ql);
int i = aa.Tables[0].Rows.Count;
if (i > 0)
{
treeView1.Nodes.Clear();
for (int n = 0; n < i; n++)
{
TreeNode mytreenode = new TreeNode(aa.Tables[0].Rows[n][0].ToString());
treeView1.Nodes.Add(mytreenode);
}
}
}
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 fm3 = new Form3();
fm3.Show();
}
private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form5 fm5 = new Form5();
fm5.Show();
}
private void 增加游客ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 fm4 = new Form4();
fm4.Show();
}
}
}
三、form3设计
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a, b, c;
a = ClassLibrary1.DataAccess.a;//身份a
b = ClassLibrary1.DataAccess.b; //账号b
c = ClassLibrary1.DataAccess.c;//密码c
if (textBox1.Text == "")
{
MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (textBox1.Text != textBox2.Text) { MessageBox.Show("两次输入密码不相同", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else if (textBox1.Text == c)
{
MessageBox.Show("密码和原密码相同", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
DialogResult dr = MessageBox.Show("你真的想要修改密码吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
string ql;
ql = "update b1 set mima='" + textBox1.Text + "' where zhanghao='" + b + "'";
ClassLibrary1.DataAccess.Update(ql);
MessageBox.Show("更新成功" , "提示");
textBox1.Text = "";
textBox2.Text = "";
}
else
{
textBox1.Text = "";
textBox2.Text = "";
}
}
}
}
}
四、form4的设计:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("账号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (textBox2.Text=="") { MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else
{
DialogResult dr = MessageBox.Show("你真的想要添加吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
string ql;
ql = "insert into b1 values( '" + textBox1.Text + "', '" + textBox2.Text + "','游客')";
ClassLibrary1.DataAccess.Update(ql);
MessageBox.Show("成功" , "提示");
textBox1.Text = "";
textBox2.Text = "";
}
else
{
textBox1.Text = "";
textBox2.Text = "";
}
}
}
}
}
五、form5的设计:
主要控件:datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
DataSet ds = new DataSet();
private void Form5_Load(object sender, EventArgs e)
{
string ql;
ql = "select zhanghao,mima,shenfen from b1 where shenfen='游客'";
//DataSet ds = new DataSet();
ds = ClassLibrary1.DataAccess.GetData(ql);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("你真的想删除?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
string a = dataGridView1.CurrentRow.Cells[0].Value.ToString();
string ql;
ql = "delete from b1 where zhanghao='" + a + "'";
ClassLibrary1.DataAccess.Update(ql);
ql = "select zhanghao,mima,shenfen from b1 where shenfen='游客'";
ds = ClassLibrary1.DataAccess.GetData(ql);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
}
六、建立classlibrary1,在里面添加一个数据库类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace ClassLibrary1
{
public class DataAccess
{
public static string a;
public static string b;
public static string c;
public static DataSet GetData(string sql)
{
string conns = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(conns);
conn.Open();
DataSet ds = new DataSet();
System.Data.OleDb.OleDbDataAdapter oda = new System.Data.OleDb.OleDbDataAdapter(sql, conn.ConnectionString);
oda.Fill(ds);
return ds;
}
public static void Update(string sql)
{
string conns = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(conns);
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
七、数据库的设计
这里简单起见,数据库就是access。它里面有两张表b1和b2(内容自己增加):
b1的设计如下:
b2的设计:
总结:
用access将建好的数据库放在bin文件夹下面就可以了运行通过了。form2的节点(如图)是为了自己看见这个控件是treeview添加的三个节点,在运行的时候treeview的节点是自动根据数据库里的内容自己生成的,那三个节点被清除了!