• 显示详细窗体加查询按键


    一、实现查询功能:
    (一)做数据访问类:
    使用CodeSmith生成
    配置链接字符串
    扩展相应的查询方法。
    (二)造界面
    添加、修改、删除
    下拉列表,文本框,查询
    ListView
    (三)写代码:
    1.查询所有的,显示在ListView中。
    a.取数据——调用后端不同的方法,返回List<TeacherData>
    b.显示出来——把List<TeacherData>加载到ListView中去
    a)做列表项——ListViewItem
    (a)做主项 ListViewItem item = new ListViewItem(); item.Text=...;item.Tag=...;
    (b)加子项 item.SubItems.Add(子项内容);
    b)加到列表上——ListView1.Items.Add()

    2.根据下拉列表选中的项进行查询。
    a.取数据——调用后端不同的方法,返回List<TeacherData>
    b.显示出来。——把List<TeacherData>加载到ListView中去


    二、实现添加功能:
    1.做添加窗体:
    做界面——略
    写代码:
    a.填充“部门”下拉列表
    //取部门所有数据
    //绑定到部门下拉列表中去。

    b.编写“添加”按钮的代码。
    //取界面上的数据,放到TeacherData对象中去
    //把造出来的TeacherData对象扔到数据库中去
    //返回DialogResult

    2.在主界面中调用显示添加窗体:
    以对话框形式显示出添加窗体。接收返回来的DialogResult
    如果返回的是OK,就调用ShowTeacher()方法重新查遍数据库,显示在列表中刷新


    三、实现修改功能:
    1.做修改窗体
    做界面---略
    写代码
    a.填充部门下拉列表。
    //取数据;
    //填进去
    b.初始化老师数据。接收要修改主键,查询要修改的记录,放在修改窗体中。
    //获取主键
    定义个成员变量,用来保存传来的主键值。 private string _Key = "";
    定义个构造函数,接收传来的值,保存在上面的成员变量中 public XXX(string code){this._Key = code;}
    //查询相应的记录
    就用XXXXDA类的方法,把主键值传进去获得查询结果。
    XXXXDA da = new XXXXDA();
    XXXXData data = da.Select(_Key);

    //把查出来的结果显示在窗体上。
    把上面的查询结果放在窗体的控件中。
    xxxx.Text = data.XXXX;
    ......

    c.编写“更新”按钮中的代码。
    //获取界面上的数据
    XXXXData data = new XXXXData();
    data.xXX = xxxx;
    .....
    //送回数据库
    XXXXXDA da = new XXXXDA();
    da.Update(data);

    //返回DialogResult
    this.DialogResult = ....OK;

    2.在主界面中调用显示修改窗体。
    //获取要修改的项的主键值。
    //显示修改窗体对话框,并把主键值传过去。
    //根据修改窗体返回的DialogResult,决定是否刷新显示。

    界面内操作

     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

    查询所有的

  • 相关阅读:
    使用pycharm开发代码上传到GitLab和GitHub
    私有仓库GitLab快速入门篇
    修改Linux Operating System的时间与时区
    版本控制工具Git工具快速入门-Linux篇
    Hadoop生态圈-Oozie实战之调度shell脚本
    Hadoop生态圈-Oozie实战之逻辑调度执行多个Job
    JavaScript基础知识-相等运算符
    Hadoop生态圈-Oozie部署实战
    Tomcat定义虚拟主机案例
    Tomcat的配置文件详解
  • 原文地址:https://www.cnblogs.com/m123/p/4512096.html
Copyright © 2020-2023  润新知