C#报修系统Ⅱ
C#报修系统Ⅱ
用户需求:
1、用户可以注册,可以登录。
2、需要一个报修界面,当点击“报修”按钮时,软件会把用户报修的信息保存起来,更新报修次数,同时会清空相应的文本框,软件还要要检查所有文本框是否为空,空的话给出提示!
具体设计思路:
第一步:数据库》添加一个名为repair_info的表,此步由侯贺琦负责!
第二步:为实现用户注册,在登录界面做出改变,把之前的取消按钮改为了注册按钮,新增一个注册窗体,两个label,两个文本框,两个按钮。点击注册按钮,可以实现用户的注册。后台代码即为实现用户注册增加的一个SQL语句,以此往user_info表插入新用户记录。此步由康贺、张玉冕负责!
第三步:新增一个报修窗体,一个labUsn,用来存放登录的用户名,这个也叫做高度抽象的效果是吧,两个文本框来存放报修地点以及报修内容,一个ReportDate(DateTimePicker)控件用了存报修日期及时间,一个comboxType,用来存放报修类型。一个按钮来实现数据的插入。此步丁志愿、李锦城负责!
第四步:为了实现用户报修次数的更新,首先要看这个用户是否是第一次报修,如果是第一次报修,那么SQL语句不需要写插入次数的字段,因为数据库设置为默认为1,所以在插入数据之前做个判断就行了。如果不是第一次报修,那么就看这个用户之前共有多少条报修记录,在原来的基础上+1。此步张宇负责!
--》界面设计:找张背景图,添加文字。在线PS就行,很简单!张宇负责!
团队成员及分工
团队: Blue 团队共有六人
姓名: 学号后四位: 贡献分:
张 宇(队长) 1152 1+2=3分
侯贺琦 1027 1+1.5=2.5分
丁志愿 1011 1+0.5=1.5分
李锦城 1040 1+0.5=1.5分
张玉冕 1153 0.7分
康 贺 1169 0.8分
第五步:请看代码实现↓↓↓
DBConn.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SqlClient; 6 using System.Data; 7 8 namespace sixth 9 { 10 class DBConn 11 { 12 //连接字符串 13 public static string connStr = "Data Source=ZHANGYU;Initial Catalog=repair;Integrated Security=True;Pooling=False"; 14 public static SqlConnection conn = new SqlConnection(connStr); 15 //读取数据 16 public static DataSet getData(string sqlStr) 17 { 18 conn.Open(); 19 SqlDataAdapter ada = new SqlDataAdapter(sqlStr, conn); 20 DataSet ds = new DataSet(); 21 ds.Clear(); 22 ada.Fill(ds); 23 conn.Close(); 24 return ds; 25 } 26 //更新数据 27 public static DataSet upData(string sqlStr) 28 { 29 try 30 { 31 conn.Open(); 32 SqlCommand comm = new SqlCommand(sqlStr, conn); 33 comm.CommandType = CommandType.Text; 34 comm.ExecuteNonQuery();//执行sql语句 35 conn.Close(); 36 } 37 catch 38 { 39 conn.Close(); 40 } 41 return null; 42 } 43 //判断是否更新记录 44 public static bool PDData(string sqlStr) 45 { 46 try 47 { 48 conn.Open(); 49 SqlCommand comm = new SqlCommand(sqlStr, conn); 50 comm.CommandType = CommandType.Text; 51 comm.ExecuteNonQuery();//执行sql语句 52 conn.Close(); 53 return true; 54 } 55 catch 56 { 57 conn.Close(); 58 return false; 59 } 60 } 61 } 62 }
FormLogin.cs
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Data.SqlClient; 10 11 namespace sixth 12 { 13 public partial class FormLogin : Form 14 { 15 public FormLogin() 16 { 17 InitializeComponent(); 18 } 19 private void btnLogin_Click(object sender, EventArgs e) 20 { 21 try 22 { 23 if (txtUsn.Text.Trim() == "") 24 { 25 labMessage.Text ="用户名不能为空!"; 26 txtUsn.Focus();//获取焦点 27 return; 28 } 29 else if (txtPwd.Text.Trim() == "") 30 { 31 labMessage.Text ="密码不能为空!"; 32 txtPwd.Focus(); 33 return; 34 } 35 string sqlStr = "select userName,passWord from user_info where userName='"+txtUsn.Text+"'"; 36 DataSet ds = DBConn.getData(sqlStr); 37 if(ds.Tables[0].Rows.Count==0) 38 { 39 labMessage.Text = "用户名不存在!请重新输入"; 40 txtUsn.Text = "";//文本框置空 41 txtPwd.Text = ""; 42 txtUsn.Focus(); 43 } 44 else if (ds.Tables[0].Rows[0][1].ToString() == txtPwd.Text.Trim()) 45 { 46 FormReport frmReport = new FormReport(); 47 frmReport.labUsn.Text = txtUsn.Text.Trim(); 48 labMessage.Text = "恭喜您已成功登录!"; 49 this.Hide(); 50 frmReport.Show(); 51 } 52 else 53 { 54 labMessage.Text = "密码错误!请重新输入!"; 55 txtPwd.Text = ""; 56 txtPwd.Focus(); 57 } 58 } 59 catch (Exception ex) 60 { 61 labMessage.Text = "登录异常:" + ex.Message; 62 txtUsn.Text = ""; 63 txtPwd.Text = ""; 64 txtUsn.Focus(); 65 } 66 finally 67 { 68 DBConn.conn.Close();//最重要的是要关闭数据库! 69 } 70 } 71 private void txtPwd_KeyDown(object sender, KeyEventArgs e) 72 { 73 if (e.KeyCode == Keys.Enter) 74 { 75 btnLogin_Click(sender, e); 76 } 77 } 78 private void btnRegist_Click(object sender, EventArgs e) 79 { 80 FormRegist frmRegist = new FormRegist(); 81 frmRegist.Show(); 82 } 83 } 84 }
FormRegist.cs
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Data.SqlClient; 10 11 namespace sixth 12 { 13 public partial class FormRegist : Form 14 { 15 public FormRegist() 16 { 17 InitializeComponent(); 18 } 19 void ClearAll() 20 { 21 txtUsn.Text = ""; 22 txtPwd.Text = ""; 23 } 24 bool userName(string userName) 25 { 26 string sqlStr = "select userName from user_info where userName='" + txtUsn.Text.Trim() + "'"; 27 DataSet ds = DBConn.getData(sqlStr); 28 if (ds.Tables[0].Rows.Count == 0) 29 { 30 return false; 31 } 32 return true; 33 } 34 private void btnCancel_Click(object sender, EventArgs e) 35 { 36 if (MessageBox.Show("确认取消注册?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) 37 { 38 this.Close(); 39 } 40 } 41 private void txtPwd_KeyDown(object sender, KeyEventArgs e) 42 { 43 if (e.KeyCode == Keys.Enter) 44 { 45 btnRegist_Click(sender, e); 46 } 47 } 48 private void btnRegist_Click(object sender, EventArgs e) 49 { 50 try 51 { 52 if (txtUsn.Text.Trim() == "") 53 { 54 MessageBox.Show("用户名不能为空!", "提示"); 55 txtUsn.Focus(); 56 } 57 else if (txtPwd.Text.Trim() == "") 58 { 59 MessageBox.Show("密码不能为空!", "提示"); 60 txtPwd.Focus(); 61 } 62 else if (userName(txtUsn.Text.Trim())) 63 { 64 MessageBox.Show("用户名已存在!", "提示"); 65 ClearAll(); 66 } 67 else 68 { 69 string sqlStr = "insert into user_info values('" + txtUsn.Text.Trim() + "','" + txtPwd.Text.Trim() + "')"; 70 if (DBConn.PDData(sqlStr)) 71 { 72 MessageBox.Show("用户名为:" + txtUsn.Text + "注册成功!"); 73 } 74 this.Close(); 75 } 76 } 77 catch (Exception ex) 78 { 79 DBConn.conn.Close(); 80 MessageBox.Show(ex.Message); 81 ClearAll(); 82 } 83 } 84 } 85 }
FormReport.cs
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Data.SqlClient; 10 11 namespace sixth 12 { 13 public partial class FormReport : Form 14 { 15 public FormReport() 16 { 17 InitializeComponent(); 18 } 19 void ClearAll() 20 { 21 txtAddress.Text = ""; 22 txtContent.Text = ""; 23 ReportDate.Value = DateTime.Now; 24 comboxType.SelectedIndex = -1; 25 } 26 private void btnReport_Click(object sender, EventArgs e) 27 { 28 try 29 { 30 if (txtAddress.Text.Trim() == "") 31 { 32 MessageBox.Show("报修地点不能为空", "提示"); 33 txtAddress.Focus(); 34 return; 35 } 36 else if (txtContent.Text.Trim() == "") 37 { 38 MessageBox.Show("报修内容不能为空", "提示"); 39 txtContent.Focus(); 40 return; 41 } 42 else if (comboxType.SelectedIndex == -1) 43 { 44 MessageBox.Show("请选择报修类型", "提示"); 45 comboxType.Focus(); 46 return; 47 } 48 string sqlStr1 = "select userName,repairCount from repair_info where userName='"+labUsn.Text+"'"; 49 DataSet ds = DBConn.getData(sqlStr1); 50 if (ds.Tables[0].Rows.Count==0)//如果ds里面不存在数据,说明这个用户第一次报修,直接插入新的记录 51 { 52 string sqlStr; 53 sqlStr = "insert into repair_info(userName,repairType,repairAddress,repairContent,repairDate) values('" + labUsn.Text + "','" + comboxType.SelectedItem.ToString() + "','" + txtAddress.Text.Trim() + "','" + txtContent.Text.Trim() + "','" + ReportDate.Value.ToString() + "')"; 54 DBConn.upData(sqlStr); 55 MessageBox.Show("报修成功!"); 56 ClearAll(); 57 } 58 else//否则的话就是repair_info表里已经有了labUsn里的这个用户。目的就是更新这个用户的报修次数,并插入新的记录 59 { 60 string sqlStr; 61 string count = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][1].ToString(); 62 sqlStr = "insert into repair_info(userName,repairType,repairAddress,repairContent,repairDate,repairCount) values('" + labUsn.Text + "','" + comboxType.SelectedItem.ToString() + "','" + txtAddress.Text.Trim() + "','" + txtContent.Text.Trim() + "','" + ReportDate.Value.ToString() + "','" + (int.Parse(count)+1) + "')"; 63 //更新语句:sqlStr = "update repair_info set repairType ='" + comboxType.SelectedItem.ToString() + "',repairAddress='" + txtAddress.Text.Trim() + "',repairContent='" + txtContent.Text.Trim() + "',repairDate='" + ReportDate.Value + "',repairCount='" + (int.Parse(ds.Tables[0].Rows[0][1].ToString()) + 1) + "'where userName='" + labUsn.Text + "'"; 64 DBConn.upData(sqlStr); 65 MessageBox.Show("报修成功!"); 66 ClearAll(); 67 } 68 } 69 catch (Exception ex) 70 { 71 DBConn.conn.Close(); 72 MessageBox.Show(ex.Message); 73 } 74 } 75 private void FormReport_FormClosing(object sender, FormClosingEventArgs e) 76 { 77 if (MessageBox.Show("您确认要退出吗?", "退出系统", MessageBoxButtons.OKCancel) == DialogResult.OK) 78 { 79 this.Dispose(); 80 Application.Exit(); 81 } 82 else 83 e.Cancel = true; 84 } 85 } 86 }
运行:
1.当用户名不存在时,注册一个新用户。
2.报修登记,提示报修成功。并且labUsn显示用户名。
3.因为登陆窗体为主窗体,之前Hide了,所以在最后窗体关闭的时候,终止应用程序。
PSP耗时分析:
团队编程总结:
这是这个项目的第二次完善,因为上次封装的时候,只是把连接字符串封装了,而没有把数据的读取封装,所以这次对数据库的访问和操作都封装到了DBConn类里,以便于调用!之前我们基本上没有注重界面这一块,看别人做的项目界面都设计的挺有个性哈,不知道这个重不重要,但这次我们也试着美化了一下界面。
个人总结:
张宇:个人认为--》其实次数的更新才是这次作业最麻烦的地方,本来呢我是直接用update语句来进行更新的,但是更新的话就不能插入新纪录了。并且,SQL语句特别容易写错。我原本以为要求的是要把次数放到repair_info表里的,所以还是用了insert语句。没关系,这样的话一次一次进行改进,不是更好吗,而且我们学到的东西也会更多。毕竟对待事物的方法不止一种,有的只是内容不同罢了!
侯贺琦:
队长仍分配给我们任务。我负责数据库连接这方面,仍然是我薄弱的一方面。队长说一个团队,要做好一个项目,要有十分全面的考虑以及分析。牛老师说作为一个学计算机的,不会写代码就像学音乐的不懂五线谱一样,还是要多努力敲代码的。代码是我们让计算机创造生产力的必须工具。我对他的话很信服,觉得他说的很对,所以我下面也会认真的敲代码,会多看书上的例题,多练习。接下来也会配合队长完善这个程序。还是要感谢队长,感谢他相信我让我负责数据库这一块。我相信我们的团队会完成得很出色。we are a team。we are 伐木累。