码迷,mamicode.com
首页 > Windows程序 > 详细

显示详细窗体加查询按键

时间:2015-05-18 16:08:27      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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.Threading.Tasks;
 9 using System.Windows.Forms;
10 using DA;
11 using Data;
12 
13 namespace WindowsFormsApplication1
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21         //取数据,放入表格
22         private void ShowTeacher()
23         {
24             //取数据
25             List<TeacherData> list = new TeacherDA().Select();
26             DisplayInListView(list);
27         }
28         private void DisplayInListView(List<TeacherData>list)
29         {
30             //
31             listView1.Items.Clear();
32             if (list ==null)
33             
34                 return;
35             //送进去
36             foreach (TeacherData data in list)
37             {
38                 //造ListViewItem
39                 //造主项
40                 ListViewItem li = new ListViewItem();
41                 li.Text = data.Tname;
42                 li.Tag = data.Tno;
43                 //造子项
44                 li.SubItems.Add(data.Tsex);
45                 li.SubItems.Add(data.Tbirthday.ToString("yyyy年MM月dd日"));
46 
47                 li.SubItems.Add(data.DepartmentData.Name);
48                 //添加进去
49                 listView1.Items.Add(li);
50 
51             }
52         }
53         private void Form1_Load(object sender, EventArgs e)
54         {
55             //加载所有数据
56             ShowTeacher();
57             //设置下拉列表定位第一项上
58             comboBox1.SelectedIndex = 0;
59         }
60 
61         private void butFind_Click(object sender, EventArgs e)
62         {
63             List<TeacherData> list = new List<TeacherData>();
64             switch (comboBox1.SelectedIndex)
65             {
66                 case 0:
67                     list = new TeacherDA().Select();
68                     break;
69                 case 1:
70                     list = new TeacherDA().SelectByTName(txt.Text);
71                     break;
72                 case 2:
73                     list = new TeacherDA().SelectByTdep(txt.Text );
74                     break;
75                 case 3:
76                     list = new TeacherDA().SelectByTSex(txt.Text );
77                     break;
78 
79                 default:
80                     break;
81             }
82             DisplayInListView(list);
83         }
84     }
85 }
界面内操作

技术分享

技术分享
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using DA;
 5 namespace Data
 6 {
 7     #region Teacher
 8 
 9     /// <summary>
10     /// Teacher class is the entity class of table teacher 
11     /// </summary>
12     public class TeacherData : System.IComparable<TeacherData>
13     {
14         #region====Private Variables====
15         
16         protected string _Tno;
17         protected string _Tname;
18         protected string _Tsex;
19         protected DateTime _Tbirthday;
20         protected string _Tdep;
21         protected DepartmentData _DepartmentData;
22         
23         
24         
25         #endregion
26 
27 
28         #region====Public Properties====
29 
30         public string Tno
31         {
32             get { return _Tno; }
33             set {_Tno = value; }
34         }
35         public string Tname
36         {
37             get { return _Tname; }
38             set {_Tname = value; }
39         }
40         public string Tsex
41         {
42             get { return _Tsex; }
43             set {_Tsex = value; }
44         }
45         public DateTime Tbirthday
46         {
47             get { return _Tbirthday; }
48             set { _Tbirthday = value; }
49         }
50         public string Tdep
51         {
52             get { return _Tdep; }
53             set {_Tdep = value; }
54         }
55         
56         public DepartmentData DepartmentData
57         {
58             get
59             {
60                 if(_DepartmentData == null)
61                 {
62                     _DepartmentData = new DepartmentDA().Select(_Tdep);
63                 }
64                 return _DepartmentData;
65             }
66             set { _DepartmentData = value; }
67         }
68         
69 
70 
71         #endregion
72         
73         public int CompareTo(TeacherData obj)
74         {
75             throw new NotImplementedException();
76         }
77     }
78 
79     #endregion
80         
81 }
实体类 teacher
技术分享
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using DA;
 5 namespace Data
 6 {
 7     #region Department
 8 
 9     /// <summary>
10     /// Department class is the entity class of table department 
11     /// </summary>
12     public class DepartmentData : System.IComparable<DepartmentData>
13     {
14         #region====Private Variables====
15         
16         protected string _Code;
17         protected string _Name;
18         protected string _Memo;
19         
20         protected List<TeacherData> _Tdepteachers;
21         
22         
23         #endregion
24 
25 
26         #region====Public Properties====
27 
28         public string Code
29         {
30             get { return _Code; }
31             set {_Code = value; }
32         }
33         public string Name
34         {
35             get { return _Name; }
36             set {_Name = value; }
37         }
38         public string Memo
39         {
40             get { return _Memo; }
41             set {_Memo = value; }
42         }
43         
44         
45 
46         public List<TeacherData> Tdepteachers
47         {
48             get
49             {
50                 if (_Tdepteachers==null)
51                 {
52                     _Tdepteachers = new TeacherDA().SelectByTdep(_Code);
53                 }
54                 return _Tdepteachers;
55             }
56             set { _Tdepteachers = value; }
57         }
58 
59         #endregion
60         
61         public int CompareTo(DepartmentData obj)
62         {
63             throw new NotImplementedException();
64         }
65     }
66 
67     #endregion
68         
69 }
实体类 department
技术分享
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using Data;
  7 namespace DA
  8 {
  9     public class TeacherDA
 10     {
 11         private SqlConnection _Conn;
 12         private SqlCommand _Cmd;
 13         public TeacherDA()
 14         {
 15             _Conn = new DBConnection().Connection;
 16             _Cmd = _Conn.CreateCommand();
 17         }
 18         public List<TeacherData> Select()
 19         {
 20             List<TeacherData> list = null;
 21             _Cmd.CommandText = "select * from teacher";
 22             try
 23             {
 24                 _Conn.Open();
 25                 SqlDataReader dr = _Cmd.ExecuteReader();
 26                 while(dr.Read())
 27                 {
 28                     if(list == null)
 29                     {
 30                         list = new List<TeacherData>();
 31                     }
 32                     TeacherData data = new TeacherData();
 33                     data.Tno = (String)dr["tno"];
 34                     data.Tname = (String)dr["tname"];
 35                     data.Tsex = (String)dr["tsex"];
 36                     data.Tbirthday = (DateTime)dr["tbirthday"];
 37                     data.Tdep = (String)dr["tdep"];
 38                     list.Add(data);
 39                 }
 40                 dr.Close();
 41                 dr.Dispose();
 42             }
 43             catch(Exception ex)
 44             {
 45                 throw ex;
 46             }
 47             finally
 48             {
 49                 _Conn.Close();
 50                 _Cmd.Dispose();
 51                 _Conn.Dispose();
 52             }
 53             return list;
 54         }
 55         
 56         
 57 
 58         public TeacherData Select(string tno)
 59         {
 60             TeacherData data = null;
 61             _Cmd.CommandText = "select * from teacher where tno = @tno";
 62             _Cmd.Parameters.Clear();
 63             _Cmd.Parameters.AddWithValue("@tno",tno);
 64             try
 65             {
 66                 _Conn.Open();
 67                 SqlDataReader dr = _Cmd.ExecuteReader();
 68                 while(dr.Read())
 69                 {
 70                     data = new TeacherData();
 71                     data.Tno = (String)dr["tno"];
 72                     data.Tname = (String)dr["tname"];
 73                     data.Tsex = (String)dr["tsex"];
 74                     data.Tbirthday = (DateTime)dr["tbirthday"];
 75                     data.Tdep = (String)dr["tdep"];
 76                 }
 77                 dr.Close();
 78                 dr.Dispose();
 79             }
 80             catch(Exception ex)
 81             {
 82                 throw ex;
 83             }
 84             finally
 85             {
 86                 _Conn.Close();
 87                 _Cmd.Dispose();
 88                 _Conn.Dispose();
 89             }
 90             return data;
 91         }
 92         
 93         public List<TeacherData> SelectByTdep(string tdep)
 94         {
 95             List<TeacherData> list = null;
 96             _Cmd.CommandText = "select * from teacher where tdep=@tdep";
 97             _Cmd.Parameters.AddWithValue("@tdep",tdep);
 98             try
 99             {
100                 _Conn.Open();
101                 SqlDataReader dr = _Cmd.ExecuteReader();
102                 while(dr.Read())
103                 {
104                     if(list == null)
105                     {
106                         list = new List<TeacherData>();
107                     }
108                     TeacherData data = new TeacherData();
109                     data.Tno = (String)dr["tno"];
110                     data.Tname = (String)dr["tname"];
111                     data.Tsex = (String)dr["tsex"];
112                     data.Tbirthday = (DateTime)dr["tbirthday"];
113                     data.Tdep = (String)dr["tdep"];
114                     list.Add(data);
115                 }
116                 dr.Close();
117                 dr.Dispose();
118             }
119             catch(Exception ex)
120             {
121                 throw ex;
122             }
123             finally
124             {
125                 _Conn.Close();
126                 _Cmd.Dispose();
127                 _Conn.Dispose();
128             }
129             return list;
130         }
131 
132         public List<TeacherData> SelectByTName(string name)
133         {
134             List<TeacherData> list = null;
135             _Cmd.CommandText = "select * from teacher where tname like @tname";
136             _Cmd.Parameters.AddWithValue("@tname", "%" + name + "%");
137             try
138             {
139                 _Conn.Open();
140                 SqlDataReader dr = _Cmd.ExecuteReader();
141                 while (dr.Read())
142                 {
143                     if (list == null)
144                     {
145                         list = new List<TeacherData>();
146                     }
147                     TeacherData data = new TeacherData();
148                     data.Tno = (String)dr["tno"];
149                     data.Tname = (String)dr["tname"];
150                     data.Tsex = (String)dr["tsex"];
151                     data.Tbirthday = (DateTime)dr["tbirthday"];
152                     data.Tdep = (String)dr["tdep"];
153                     list.Add(data);
154                 }
155                 dr.Close();
156                 dr.Dispose();
157             }
158             catch (Exception ex)
159             {
160                 throw ex;
161             }
162             finally
163             {
164                 _Conn.Close();
165                 _Cmd.Dispose();
166                 _Conn.Dispose();
167             }
168             return list;
169         }
170 
171         public List<TeacherData> SelectByTSex(string sex)
172         {
173             List<TeacherData> list = null;
174             _Cmd.CommandText = "select * from teacher where tsex = @tsex";
175             _Cmd.Parameters.AddWithValue("@tsex", sex);
176             try
177             {
178                 _Conn.Open();
179                 SqlDataReader dr = _Cmd.ExecuteReader();
180                 while (dr.Read())
181                 {
182                     if (list == null)
183                     {
184                         list = new List<TeacherData>();
185                     }
186                     TeacherData data = new TeacherData();
187                     data.Tno = (String)dr["tno"];
188                     data.Tname = (String)dr["tname"];
189                     data.Tsex = (String)dr["tsex"];
190                     data.Tbirthday = (DateTime)dr["tbirthday"];
191                     data.Tdep = (String)dr["tdep"];
192                     list.Add(data);
193                 }
194                 dr.Close();
195                 dr.Dispose();
196             }
197             catch (Exception ex)
198             {
199                 throw ex;
200             }
201             finally
202             {
203                 _Conn.Close();
204                 _Cmd.Dispose();
205                 _Conn.Dispose();
206             }
207             return list;
208         }
209 
210        
211         
212         public void Insert(TeacherData data)
213         {
214             _Cmd.CommandText = "insert into teacher values(@tno,@tname,@tsex,@tbirthday,@tdep)";
215             _Cmd.Parameters.Clear();
216             _Cmd.Parameters.AddWithValue("@tno",data.Tno);
217             _Cmd.Parameters.AddWithValue("@tname",data.Tname);
218             _Cmd.Parameters.AddWithValue("@tsex",data.Tsex);
219             _Cmd.Parameters.AddWithValue("@tbirthday",data.Tbirthday);
220             _Cmd.Parameters.AddWithValue("@tdep",data.Tdep);
221             try
222             {
223                 _Conn.Open();
224                 _Cmd.ExecuteNonQuery();
225             }
226             catch(Exception ex)
227             {
228                 throw ex;
229             }
230             finally
231             {
232                 _Conn.Close();
233                 _Cmd.Dispose();
234                 _Conn.Dispose();
235             }
236         }
237         public void Update(TeacherData data)
238         {
239             _Cmd.CommandText = "update teacher set tname = @tname,tsex = @tsex,tbirthday = @tbirthday,tdep = @tdep where tno = @tno";
240             _Cmd.Parameters.Clear();
241             _Cmd.Parameters.AddWithValue("@tno",data.Tno);
242             _Cmd.Parameters.AddWithValue("@tname",data.Tname);
243             _Cmd.Parameters.AddWithValue("@tsex",data.Tsex);
244             _Cmd.Parameters.AddWithValue("@tbirthday",data.Tbirthday);
245             _Cmd.Parameters.AddWithValue("@tdep",data.Tdep);
246             try
247             {
248                 _Conn.Open();
249                 _Cmd.ExecuteNonQuery();
250             }
251             catch(Exception ex)
252             {
253                 throw ex;
254             }
255             finally
256             {
257                 _Conn.Close();
258                 _Cmd.Dispose();
259                 _Conn.Dispose();
260             }
261         }
262         public static void DeleteByTdep(string tdep)
263         {
264             SqlConnection conn = new SqlConnection(DBConnection.ConnectionString);
265             SqlCommand cmd = conn.CreateCommand();
266             cmd.CommandText = "delete from teacher where tdep=@tdep";
267             cmd.Parameters.AddWithValue("@tdep",tdep);
268             try
269             {
270                 conn.Open();
271                 cmd.ExecuteNonQuery();
272             }
273             catch(Exception ex)
274             {
275                 throw ex;
276             }
277             finally
278             {
279                 cmd.Dispose();
280                 conn.Close();
281             }
282         }
283         public static void Delete(string tno)
284         {
285             SqlConnection conn = new SqlConnection(DBConnection.ConnectionString);
286             SqlCommand cmd = conn.CreateCommand();
287             cmd.CommandText = "delete from teacher where tno = @tno";
288             cmd.Parameters.Clear();
289             cmd.Parameters.AddWithValue("@tno",tno);
290             try
291             {
292                 conn.Open();
293                 cmd.ExecuteNonQuery();
294             }
295             catch(Exception ex)
296             {
297                 throw ex;
298             }
299             finally
300             {
301                 cmd.Dispose();
302                 conn.Close();
303             }
304         }
305         public void Delete(TeacherData data)
306         {
307             _Cmd.CommandText = "delete from teacher where tno = @tno";
308             _Cmd.Parameters.Clear();
309             _Cmd.Parameters.AddWithValue("@tno",data.Tno);
310             try
311             {
312                 _Conn.Open();
313                 _Cmd.ExecuteNonQuery();
314             }
315             catch(Exception ex)
316             {
317                 throw ex;
318             }
319             finally
320             {
321                 _Conn.Close();
322                 _Cmd.Dispose();
323                 _Conn.Dispose();
324             }
325         }
326     }
327 }
数据访问类 teacher
技术分享
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using Data;
  7 namespace DA
  8 {
  9     public class DepartmentDA
 10     {
 11         private SqlConnection _Conn;
 12         private SqlCommand _Cmd;
 13         public DepartmentDA()
 14         {
 15             _Conn = new DBConnection().Connection;
 16             _Cmd = _Conn.CreateCommand();
 17         }
 18         public List<DepartmentData> Select()
 19         {
 20             List<DepartmentData> list = null;
 21             _Cmd.CommandText = "select * from department";
 22             try
 23             {
 24                 _Conn.Open();
 25                 SqlDataReader dr = _Cmd.ExecuteReader();
 26                 while(dr.Read())
 27                 {
 28                     if(list == null)
 29                     {
 30                         list = new List<DepartmentData>();
 31                     }
 32                     DepartmentData data = new DepartmentData();
 33                     data.Code = (String)dr["code"];
 34                     data.Name = (String)dr["name"];
 35                     data.Memo = (String)dr["memo"];
 36                     list.Add(data);
 37                 }
 38                 dr.Close();
 39                 dr.Dispose();
 40             }
 41             catch(Exception ex)
 42             {
 43                 throw ex;
 44             }
 45             finally
 46             {
 47                 _Conn.Close();
 48                 _Cmd.Dispose();
 49                 _Conn.Dispose();
 50             }
 51             return list;
 52         }
 53         
 54         public DepartmentData Select(string code)
 55         {
 56             DepartmentData data = null;
 57             _Cmd.CommandText = "select * from department where code = @code";
 58             _Cmd.Parameters.Clear();
 59             _Cmd.Parameters.AddWithValue("@code",code);
 60             try
 61             {
 62                 _Conn.Open();
 63                 SqlDataReader dr = _Cmd.ExecuteReader();
 64                 while(dr.Read())
 65                 {
 66                     data = new DepartmentData();
 67                     data.Code = (String)dr["code"];
 68                     data.Name = (String)dr["name"];
 69                     data.Memo = (String)dr["memo"];
 70                 }
 71                 dr.Close();
 72                 dr.Dispose();
 73             }
 74             catch(Exception ex)
 75             {
 76                 throw ex;
 77             }
 78             finally
 79             {
 80                 _Conn.Close();
 81                 _Cmd.Dispose();
 82                 _Conn.Dispose();
 83             }
 84             return data;
 85         }
 86         
 87         
 88         public void Insert(DepartmentData data)
 89         {
 90             _Cmd.CommandText = "insert into department values(@code,@name,@memo)";
 91             _Cmd.Parameters.Clear();
 92             _Cmd.Parameters.AddWithValue("@code",data.Code);
 93             _Cmd.Parameters.AddWithValue("@name",data.Name);
 94             _Cmd.Parameters.AddWithValue("@memo",data.Memo);
 95             try
 96             {
 97                 _Conn.Open();
 98                 _Cmd.ExecuteNonQuery();
 99             }
100             catch(Exception ex)
101             {
102                 throw ex;
103             }
104             finally
105             {
106                 _Conn.Close();
107                 _Cmd.Dispose();
108                 _Conn.Dispose();
109             }
110         }
111         public void Update(DepartmentData data)
112         {
113             _Cmd.CommandText = "update department set name = @name,memo = @memo where code = @code";
114             _Cmd.Parameters.Clear();
115             _Cmd.Parameters.AddWithValue("@code",data.Code);
116             _Cmd.Parameters.AddWithValue("@name",data.Name);
117             _Cmd.Parameters.AddWithValue("@memo",data.Memo);
118             try
119             {
120                 _Conn.Open();
121                 _Cmd.ExecuteNonQuery();
122             }
123             catch(Exception ex)
124             {
125                 throw ex;
126             }
127             finally
128             {
129                 _Conn.Close();
130                 _Cmd.Dispose();
131                 _Conn.Dispose();
132             }
133         }
134         public static void Delete(string code)
135         {
136             SqlConnection conn = new SqlConnection(DBConnection.ConnectionString);
137             SqlCommand cmd = conn.CreateCommand();
138             cmd.CommandText = "delete from department where code = @code";
139             cmd.Parameters.Clear();
140             cmd.Parameters.AddWithValue("@code",code);
141             try
142             {
143                 conn.Open();
144                 cmd.ExecuteNonQuery();
145             }
146             catch(Exception ex)
147             {
148                 throw ex;
149             }
150             finally
151             {
152                 cmd.Dispose();
153                 conn.Close();
154             }
155         }
156         public void Delete(DepartmentData data)
157         {
158             _Cmd.CommandText = "delete from department where code = @code";
159             _Cmd.Parameters.Clear();
160             _Cmd.Parameters.AddWithValue("@code",data.Code);
161             try
162             {
163                 _Conn.Open();
164                 _Cmd.ExecuteNonQuery();
165             }
166             catch(Exception ex)
167             {
168                 throw ex;
169             }
170             finally
171             {
172                 _Conn.Close();
173                 _Cmd.Dispose();
174                 _Conn.Dispose();
175             }
176         }
177     }
178 }
数据访问类 department
技术分享
 1 using System;
 2 using System.Data;
 3 using System.Data.SqlClient;
 4 namespace DA
 5 {
 6     public class DBConnection
 7     {
 8         private SqlConnection _Conn;
 9         public DBConnection()
10         {
11             String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
12             
13             _Conn = new SqlConnection(connectionString);
14         }
15         public SqlConnection Connection
16         {
17             get
18             {
19                 return _Conn;
20             }
21         }
22         public static string ConnectionString
23         {
24             get
25             {
26                 String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
27                 return connectionString;
28             }
29         }
30     }
31 }
链接 DBConnection
技术分享
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3     <connectionStrings>
4         <add name ="conn" connectionString="server=MA-PC;database=mydb;uid=sa;pwd=111111"/>
5     </connectionStrings>
6     <startup> 
7         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
8     </startup>
9 </configuration>
链接

  再添加引用中选择Configuration

查询所有的

技术分享

显示详细窗体加查询按键

标签:

原文地址:http://www.cnblogs.com/m123/p/4512096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!