using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Theuserservice { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DBCon db = new DBCon(); private void denglu_Click(object sender, EventArgs e) { db.dbcon(); if (textBoxname.Text != "" & textBoxpass.Text != "") { string com = "select count(*) passWord from user_info where userName='" + textBoxname.Text + "' "; SqlCommand comm = new SqlCommand(com, db.conn); int a = (int)comm.ExecuteScalar(); if (a == 0) { this.label1.Text = "用户名不存在哦!请重新输入用户名··"; //MessageBox.Show("用户名不存在!"); } else { string com1 = "select count(*) passWord from user_info where userName='" + textBoxname.Text + "' and passWord='" + textBoxpass.Text + "'"; SqlCommand comd = new SqlCommand(com1, db.conn); int a1 = (int)comd.ExecuteScalar(); if (a1 == 0) { this.label1.Text = "密码错误!请重新输入密码··"; //MessageBox.Show("密码错误哦!"); } else { this.label1.Text = "登陆成功!欢迎··"; WindowsFormsApplication1.Form3 fm = new WindowsFormsApplication1.Form3(textBoxname.Text); fm.Show(); //MessageBox.Show("登录成功!"); } } db.conn.Close(); } else { this.label1.Text = "用户名或者密码不能为空!"; } } private void button1_Click(object sender, EventArgs e) { WindowsFormsApplication1.Form2 fm = new WindowsFormsApplication1.Form2(); fm.Show(); } private void Form1_Load(object sender, EventArgs e) { } } }
Form2;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Theuserservice; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } DBCon db = new DBCon(); private void button1_Click(object sender, EventArgs e) { db.dbcon(); string name = this.txtxname.Text.Trim(); string pwd1 = this.txtpwd1.Text.Trim(); string pwd2 = this.txtpwd2.Text.Trim(); if (name == "") { MessageBox.Show("用户名不能为空!"); return; } if (pwd1 == "") { MessageBox.Show("密码不能为空!"); return; } if (pwd2 == "") { MessageBox.Show("请再次输入密码!"); return; } if (pwd1 == pwd2) { string sql = "insert into user_info (userName,passWord) values ('" + name + "','" + pwd1 + "')"; SqlCommand comd = new SqlCommand(sql, db.conn); int a1 = db.ExecuteSQL(sql); if (a1 > 0) { MessageBox.Show("注册成功,请返回登录!"); this.Hide(); Form1 fm = new Form1(); fm.Show(); } else { MessageBox.Show("请重试!"); } } else { MessageBox.Show("两次密码不一致,请重新输入!"); } } private void button2_Click(object sender, EventArgs e) { Form1 fm = new Form1(); fm.Show(); } private void Form2_Load(object sender, EventArgs e) { } } }
Form3:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Theuserservice; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form3 : Form { DBCon db = new DBCon(); public string name = ""; public Form3( string value) { InitializeComponent(); this.name = value; } private void button1_Click(object sender, EventArgs e) { if (this.txtaddress.Text.Trim()=="") { MessageBox.Show("报修失败!请输入报修地点!"); return; } if (this.txtleixing.Text.Trim() == "") { MessageBox.Show("报修失败!请输入报修内容!"); return; } string sql = "insert into repair_info (username,leixing,didian,leirong,riqi,cishu) values ('" + name + "','" + comboBox1.SelectedItem.ToString()+"','" + this.txtaddress.Text + "','" + this.txtleixing.Text + "','" + Time.Now.ToShortDateString() + "','1')"; SqlCommand comd = new SqlCommand(sql, db.conn); int a1 = db.ExecuteSQL(sql); if (a1 > 0) { MessageBox.Show("报修成功!"); this.txtleixing.Text = ""; this.txtaddress.Text = ""; this.comboBox1.Text = "电脑类"; } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void Form3_Load(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }
DBCon.cs;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; using System.Windows.Forms; namespace Theuserservice { class DBCon { public string ConnectString = "Data Source=.;Initial Catalog=repair;Integrated Security=True"; public SqlConnection conn = new SqlConnection(); public void dbcon() { try { conn = new SqlConnection(ConnectString); conn.Open(); } catch (Exception e) { MessageBox.Show("数据库连接不成功" + e.ToString()); } } public int ExecuteSQL(String SqlString) { int count = -1; SqlConnection connectionTemp = new SqlConnection(ConnectString); connectionTemp.Open(); try { SqlCommand cmd = new SqlCommand(SqlString, connectionTemp); count = cmd.ExecuteNonQuery(); } catch { count = -1; } finally { connectionTemp.Close(); } return count; } } }
数据库创建截图:
测试截图:
设计思路:
(1)了解问题。我们看了需求后,接下来就是按照增量要求进行分析,添加了一个注册界面和一个报修界面。
(2)同时新建一个报修表,名字为repair_info,确认输出内容。我们要能用代码实现对数据库的增删改查操作,以此来实现用户的注册。
(3)设计报修界面。对每快的操作流程做出设计,如何输入,如何输出,如何判断,如何提交等等。
(4)编写整体代码。 将我们确定的算法实体化,最终形成C#代码。
(5)测试并解决问题。编译运行,多次测试,看结果是否与预期相同,如果不同,那么查找问题并修改,最终形成合格的程序。
团队分工:
负责人:洪亮,负责代码规范和代码实现(3.5分)
队员:李彬,负责代码错误和PSP分析(2分)
队员:卞玉新,负责创建数据库和美化(1.5分)
队员:刘远航,负责设计思路和测试(1.5分)
队员:李田田,负责控件排版和团队总结(1.5分)
PSP耗时:
团队总结:
这次作业实现的增量较多,所以开始还是比较麻烦点的。这次是实现三个界面的对数据库进行操作,这次的增量还是主要是数据库
数据库方面的知识,简单的界面框架和不太复杂的代码对我们还是比较好做的,数据库无非就是对Connection、Command、DataReader
DataSet这些对象的操作运用。同时在这方面,我们通过C#操作数据库方面的书籍,从中还是学到了不少东西的。
然而这次的小项目 我们做的并不是很好,对于报修次数,同一用户每次报修+1这点没有完成需求。逻辑能力方面的问题,然而我们并不知道怎么解决。