A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邓超军 中级黑马   /  2012-7-22 20:43  /  2206 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我的思路是把数据保存到access数据文件里,因为时间原因,没有弄完,现把弄完的部分传上来。
部分代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.Drawing;
  8. using System.Windows.Forms;

  9. namespace 信息管理
  10. {
  11.     public partial class Form1 : Form
  12.     {   
  13.        boolean flag=false;
  14.         //登录和删除信息数据的代码
  15.         private void button1_Click(object sender, EventArgs e)
  16.         {
  17.             if (checkBox1.Checked == true)
  18.             {
  19.                 String sqlPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\Info.mdb;Persist Security Info=True;Jet OLEDB:Database Password=198168888";
  20.                 OleDbConnection conn = new OleDbConnection(sqlPath);
  21.                 conn.Open();
  22.                 String sqlStr = "select * from log where user ='" + userText.Text + "' and pswd ='" + pswdText.Text + "'";
  23.                 OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
  24.                 if (cmd.ExecuteReader().Read())
  25.                 {
  26.                     flagd = true;
  27.                     userID = userText.Text;
  28.                     label1.Visible = true;
  29.                     label1.Text = "登录成功!";
  30.                     label1.ForeColor = Color.Green;
  31.                 }
  32.                 else
  33.                 {
  34.                     label1.Visible = true;
  35.                     label1.Text = "用户名或密码错误!";
  36.                     label1.ForeColor = Color.Red;
  37.                 }
  38.                 conn.Close();
  39.             }
  40.             else
  41.             {
  42.                 String sqlPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\Info.mdb;Persist Security Info=True;Jet OLEDB:Database Password=198168888";
  43.                 OleDbConnection conn = new OleDbConnection(sqlPath);
  44.                 conn.Open();
  45.                 String sqlStr = "delete log where user ='"+userText+"'";
  46.                 OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
  47.                 cmd.ExecuteNonQuery();
  48.                 sqlStr = "delete UserInfo where user ='" + userText + "'";
  49.                 cmd = new OleDbCommand(sqlStr, conn);
  50.                 cmd.ExecuteNonQuery();
  51.                 conn.Close();
  52.             }
  53.             
  54.         }

  55.         //注册和更新数据
  56.         private void button2_Click(object sender, EventArgs e)
  57.         {
  58.             String pswdzc = pswdzcText.Text;
  59.             String rpswdzc = rpswdzcText.Text;
  60.             String userIDzc = usertextBox.Text;
  61.             String namezc=namezctext.Text;
  62.             String agezc = agezctext.Text;
  63.             String addrzc = addrzctext.Text;
  64.             String telezc = telezctext.Text;
  65.             if (pswdzc.Length < 8)
  66.             {
  67.                 pswdzclabel.Text = "密码少于8个字符!";
  68.                 pswdzclabel.ForeColor = Color.Red;
  69.                 flag = false;
  70.             }
  71.             else
  72.             {
  73.                 pswdzclabel.Text = "密码正确!";
  74.                 pswdzclabel.ForeColor = Color.Green;
  75.                 flag =true;
  76.                 if (pswdzc!=(rpswdzc))
  77.                 {
  78.                     rpswdzclabel.Text = "前后密码不一致";
  79.                     pswdzclabel.ForeColor = Color.Red;
  80.                     flag = false;
  81.                     rpswdzclabel.Visible = true;
  82.                 }
  83.                 else
  84.                 {
  85.                     rpswdzclabel.Text = "密码正确!";
  86.                     pswdzclabel.ForeColor = Color.Green;
  87.                     flag = true;
  88.                     rpswdzclabel.Visible = true;
  89.                 }
  90.             }

  91.             if (namezc == "") namezc = null;
  92.             if (agezc == "") agezc = null;
  93.             if (telezc == "") telezc =null;
  94.             if (addrzc == "") addrzc = null;
  95.             if (flag)
  96.             {
  97.                 String sqlPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\Info.mdb;Persist Security Info=True;Jet OLEDB:Database Password=198168888";
  98.                 OleDbConnection conn = new OleDbConnection(sqlPath);
  99.                 conn.Open();
  100.                 String sqlStr = "insert into UserInfo (userName,age,tele,address,user) values('"+namezc+"','"+agezc+"','"+telezc+"','"+addrzc+"','"+userIDzc+"')";
  101.                 OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
  102.                 cmd.ExecuteNonQuery();
  103.                 sqlStr="insert into log (user,pswd) values('"+userIDzc+"','"+pswdzc+"')";
  104.                 cmd = new OleDbCommand(sqlStr, conn);
  105.                 cmd.ExecuteNonQuery();
  106.                 conn.Close();
  107.             }

  108.         private void button4_Click(object sender, EventArgs e)
  109.         {
  110.             String userzc = usertextBox.Text;
  111.             if(userzc.Length<5)
  112.             {
  113.                 userlabel.Text = "用户名少于5个字符";
  114.                 userlabel.ForeColor = Color.Red;
  115.             }
  116.             else
  117.             {
  118.                 String sqlPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\Info.mdb;Persist Security Info=True;Jet OLEDB:Database Password=198168888";
  119.                 OleDbConnection conn = new OleDbConnection(sqlPath);
  120.                 conn.Open();
  121.                 try
  122.                 {
  123.                     String sqlStr = "select * from log where user ='" + userzc + "'";
  124.                     OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
  125.                     OleDbDataReader rd = cmd.ExecuteReader();
  126.                     if (rd.Read())
  127.                     {
  128.                         pswdzcText.Text = rd.GetString(2);
  129.                         rpswdzcText.Text = pswdzcText.Text;
  130.                     }
  131.                 }
  132.                 catch (System.Exception ex)
  133.                 {
  134.                        
  135.                 }
  136.                 try
  137.                 {
  138.                     String sqlStr = "select * from UserInfo where user ='" + userzc + "'";
  139.                     OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
  140.                     OleDbDataReader rd = cmd.ExecuteReader();
  141.                     if (rd.Read())
  142.                     {
  143.                         namezctext.Text = rd.GetString(1);
  144.                         agezctext.Text = rd.GetString(4);
  145.                         telezctext.Text = rd.GetString(2);
  146.                         addrzctext.Text = rd.GetString(3);
  147.                     }
  148.                 }
  149.                 catch (System.Exception ex)
  150.                 {

  151.                 }
  152.                 conn.Close();
  153.             }
  154.         }
  155. }
复制代码
我是用C#写的。

信息管理.zip

77.46 KB, 下载次数: 172

2 个回复

倒序浏览
建立楼主发到相应的帖子里面
http://bbs.itheima.com/thread-20135-1-1.html
回复 使用道具 举报
韦念欣 发表于 2012-7-22 20:47
建立楼主发到相应的帖子里面
http://bbs.itheima.com/thread-20135-1-1.html

老师,我这里在内蒙古山区,没有宽带,我用的无线网卡,上网有时限,麻烦老师看看能给分不?谢谢!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马