第4章 系统实现
4.2系统登录
本系统的登录界面如图4-1所示:
图4-1 登陆界面的实现
(1)登陆的代码的实现:
private void btlogin_Click(object sender, EventArgs e)
{ string username = null; string password = null ; if (tbusername.Text == \)
{ MessageBox.Show(\你没有输入用户名!\,\错
误提示\); return; }
if (tbmima.Text == \)
{ MessageBox.Show(\你没有输入密码!\,\错误提示\); return; }
DataSet dst = new DataSet();
string connstr = \Source=(local);Initial Catalog=Message;Integrated Security=True\;
SqlConnection con = new SqlConnection(connstr); con.Open();
string str = \登录\;
SqlDataAdapter sda = new SqlDataAdapter(str, con);
SqlCommandBuilder scd = new SqlCommand Builder(sda);
sda.Fill(dst, \);
dataGridView1.DataSource = dst.Tables [\].DefaultView;
username = dataGridView1.Rows[0].Cells[0]. Value.ToString().Trim();
password = dataGridView1.Rows[0].Cells[1]. Value.ToString().Trim();
if (tbusername.Text.Equals(username) &&
tbmima.Text.Equals(password)) { Form2 f2 = new Form2(); f2.Visible = true; this.Visible = false; } else
{ MessageBox.Show(\你输入的用户名或密码错误\,\错误提示\); tbusername.Clear(); tbmima.Clear(); }
con.Close(); }
(2)取消按钮的代码实现:
private void btcancel_Click(object sender, EventArgs e)
{ if (MessageBox.Show(\你是否真的想退出系统?\, \提示\, MessageBoxButtons.YesNo) != DialogResult.No) { this.Close(); } }
(3)修改密码按钮的实现:
private void button1_Click(object sender, EventArgs e)
{ string username = null; string password = null; if (tbusername.Text == \)
{ MessageBox.Show(\你没有输入用户名!\, \错误提示\); return; }
if (tbmima.Text == \)
{ MessageBox.Show(\你没有输入密码!\, \错误提示\); return; }
DataSet dst = new DataSet();
string connstr = \Source=(local);Initial Catalog=Message;Integrated Security=True\; SqlConnection con = new SqlConnection (connstr);
con.Open();
string str = \登录\;
SqlDataAdapter sda = new SqlDataAdapter(str, con);
SqlCommandBuilder scd = new SqlCommand Builder(sda);
sda.Fill(dst, \);
dataGridView1.DataSource = dst.Tables [\].DefaultView;
username = dataGridView1.Rows[0].Cells[0]. Value.ToString().Trim();
password = dataGridView1.Rows[0].Cells[1]. Value.ToString().Trim();
if (tbusername.Text.Equals(username) && tbmima.Text.Equals(password))
{ if (MessageBox.Show(\你确定要修改密码吗?\, \提示\, MessageBoxButtons.YesNo) == DialogResult.Yes)
{ 修改密码 mima = new 修改密码(); mima.Visible = true;
} } else
{ MessageBox.Show(\你输入的用户名或密码错误\, \错误提示\);
tbusername.Clear(); tbmima.Clear(); }
con.Close(); }
实现密码的修改的界面如图4-2所示:
图4-2 修改密码界面的实现
(1)确定按钮的代码实现:
private void button1_Click(object sender, EventArgs e)
{ string username = null; string password = null; if (textBox1.Text == \)
{ MessageBox.Show(\你没有输入密码!\, \错误提示\); return; }
if (textBox2.Text == \)
{ MessageBox.Show(\你没有输入更改密码!\, \错误提示\); return; }
if (textBox3.Text == \)
{ MessageBox.Show(\你没有再次输入更改密码!\, \错误提示\); return; }
string b = textBox2.Text.ToString().Trim(); string a = textBox3.Text.ToString().Trim(); if (b.Equals(a)==false)
{ MessageBox.Show(\你两次输入的密码不一样!\,\错误
}
DataSet dst = new DataSet();
string connstr = \Catalog=Message;Integrated Security=True\; SqlConnection con = new SqlConnection (connstr); con.Open();
string str = \登录\;
SqlDataAdapter sda = new SqlDataAdapter(str, con); SqlCommandBuilder scd = new SqlCommand Builder(sda);
sda.Fill(dst, \);
dataGridView1.DataSource = dst.Tables [\].DefaultView;
username = dataGridView1.Rows[0].Cells[0]. Value.ToString().Trim();
password = dataGridView1.Rows[0].Cells[1]. Value.ToString().Trim();
if (textBox1.Text.Equals(password)) 提示\); return;
{ String sql = \登录 set mima='\+textBox2.Text.Trim()+%username='\+username+\; Try
{ SqlCommand comm = new SqlCommand(sql, con); comm.ExecuteNonQuery(); }
catch(Exception es)
{MessageBox.Show(\修改密码失败!\,\提示\); return; } } else
{ MessageBox.Show(\你输入的密码错误\, \错误提示\); textBox1.Clear(); return; }
MessageBox.Show(\密码更改成功!\); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); con.Close(); }
(2)取消按钮的代码的实现:
private void button2_Click(object sender, EventArgs e) { this.Close(); }
4.2系统功能的实现
个人备忘录界面如下图4-3所示:
图4-3个人备忘录界面的实现
(1)实现备忘录的查询代码的实现: private void button6_Click(object sender, EventArgs e)
{ string str = null; if (textBox6.Text != \)
{ str = \备忘录 where 备忘标
题='\ + textBox6.Text + \; textBox6.Clear(); } else
if (comboBox1.Text != \) { string lei = comboBox1.Text;
if (lei.Equals(\日程表\) == false &&
lei.Equals(\纪念日\) == false && lei.Equals(\记事本\) == false && lei.Equals(\节日\) == false && lei.Equals(\备忘录\) == false)
{ MessageBox.Show(\你输入的类别不正确,请
重新输入!\, \错误提示\); return; } else
{ str = \备忘录 where 备忘类
别='\ + comboBox1.Text + \; comboBox1.Text = null; } else
if (textBox7.Text != \) { DateTime time=DateTime.Now; try
{ time = DateTime.Parse(textBox7.Text);} catch (Exception s)
{ MessageBox.Show(\你输入的日期格式不正
确\,\错误提示\); return; }
str = \备忘录 where YEAR(创建时间)=YEAR('\ + time + \创建时间)=MONTH('\ + time + \创建时间)=DAY('\ + time + \; textBox7.Clear(); }
else
{ MessageBox.Show(\你没有输入要查询的条件\, \错误提示\); return;
} DataSet ds = new DataSet();
string connstr = \Source=(local);Initial Catalog=Message;Integrated Security=True\; SqlConnection con=new SqlConnection (connstr);;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(str, con);
SqlCommandBuilder scd = new SqlCommandBuilder (sda);
sda.Fill(ds, \);
dataGridView1.DataSource = ds.Tables
[\].DefaultView; dataGridView1.Rows[0].Selected = true; try
{ textBox1.Text = dataGridView1.Rows[0]. Cells[0].Value.ToString().Trim(); textBox2.Text = dataGridView1.Rows[0]. Cells[1].Value.ToString().Trim(); textBox3.Text = dataGridView1.Rows[0]. Cells[2].Value.ToString().Trim(); textBox4.Text = dataGridView1.Rows[0]. Cells[3].Value.ToString().Trim(); textBox5.Text = dataGridView1.Rows[0]. Cells[4].Value.ToString().Trim(); DateTime shenqing = DateTime.Parse (textBox3.Text);
DateTime tiqing = DateTime.Parse (textBox4.Text);