一、计划
这个程序需要七天。
二: 需求分析:
用户故事:作为一个教练,我希望了解每一场队友和对方的比赛加分和扣分细节,以及所有队员每一场的得分与排行情况,以便于更好的对队员做出战术安排。
三:设计文档
四:代码:
public static class SqlHelper { private static readonly string constr ="server=.;database=itcast;integrated security=true"; public static int ExecuteNonQuery(string sql, params SqlParameter[] pams) { using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand comm = new SqlCommand(sql, conn)) { if (pams != null) { comm.Parameters.AddRange(pams); } conn.Open(); return comm.ExecuteNonQuery(); } } } public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pams) { SqlConnection conn = new SqlConnection(constr); using (SqlCommand comm = new SqlCommand(sql, conn)) { if (pams != null) { comm.Parameters.AddRange(pams); } conn.Open(); return comm.ExecuteReader(CommandBehavior.CloseConnection); } } public static object ExecuteScalar(string sql, params SqlParameter[] pams) { using (SqlConnection conn = new SqlConnection(constr)) { using (SqlCommand comm = new SqlCommand(sql, conn)) { if (pams != null) { comm.Parameters.AddRange(pams); } conn.Open(); return comm.ExecuteScalar(); } } } public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pams) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(sql, constr)) { if (pams != null) { adapter.SelectCommand.Parameters.AddRange(pams); } adapter.Fill(dt); } return dt; } } }
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 10 namespace WindowsFormsApplication1 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 private void DataGridviewDataBind(string sql = "select * from student") 19 { 20 dgvstudent.DataSource = SqlHelper.ExecuteDataTable(sql); 22 //DataSet ds = new DataSet(); 24 //string strConn = "data source=.;initial catalog=itcast; integrated security=true"; 25 //SqlConnection connection = new SqlConnection(strConn); 28 //SqlDataAdapter ada = new SqlDataAdapter(sql, connection); 30 //ada.Fill(ds); 31 } 32 private void btn_Click(object sender, EventArgs e) 33 { 34 StringBuilder sql = new StringBuilder(); 35 sql.Append("select * from student where 1=1"); 36 string StuNum = txtStuNum1.Text.Trim(); 37 if (!string.IsNullOrEmpty(StuNum)) 38 { 39 sql.Append(" and stuNum='" + StuNum + "'"); 40 } 41 string StuName = txtStuName1.Text.Trim(); 42 if (!string.IsNullOrEmpty(StuName)) 43 { 44 sql.Append(" and stuName like '%" + StuName + "%'"); 45 } 46 string StuClass = txtStuClass1.Text.Trim(); 47 if (!string.IsNullOrEmpty(StuClass)) 48 { 49 sql.Append(" and stuClass like '%" + StuClass + "%'"); 50 } 51 string subject = cmbsubject.Text.Trim(); 52 if (!string.IsNullOrEmpty(subject)) 53 { 54 if (subject != "全部") 55 { 56 sql.Append(" and subject like '%" + subject + "%'"); 57 58 } 59 DataGridviewDataBind(sql.ToString()); 60 61 } 62 } 63 } 64 }
五:对整个程序进行总结