WinForm笔记

企业员工管理系统  winForm
DB.cs 类文件:
           public static SqlConnection createCon()
           {      return new SqlConnection("server=.;uid=sa;pwd=;database=EmployeeManagment");   }
主窗体部分代码: main menu控件)
           private void menuItem2_Click(object sender, System.EventArgs e)
           {      ViewInfo vi=new ViewInfo();
                 vi.MdiParent=this;
                 vi.Show();       }
点击查看员工信息菜单窗体部分代码:( listView、dataGrid控件)
           private void ViewInfo_Load(object sender, System.EventArgs e)
           { //初始显示全部数据
               con=DB.createCon();
                 ds=new DataSet();
                 sda=new SqlDataAdapter("select Emp_Name,Emp_Birthday,Emp_Gender,Emp_Remark from Employee",con);
                 sda.Fill(ds,"Employee");
                 this.dataGrid1.DataSource=ds.Tables["Employee"];
            //ListView绑定数据
                 SqlDataReader dr=null;
                 ListViewItem item;
                 cmd=new SqlCommand("select Dep_Name from DepartMent",con);
                 con.Open();
                 dr=cmd.ExecuteReader();
                 while(dr.Read())
                 {    item=new ListViewItem();
                       item.Text=dr.GetValue(0).ToString();
                       this.listView1.Items.Add(item);
                 }      dr.Close();
           }
 
           private void listView1_ItemActivate(object sender, System.EventArgs e)
           {//点击部门名称,显示相应部门信息
                 string Dep_Name=this.listView1.SelectedItems[0].Text;
                 string DepID=null;
                 if(Dep_Name=="技术部")        {      DepID="1"; }
                 if(Dep_Name=="市场部")        {      DepID="2"; }
                 if(Dep_Name=="人事部")        {      DepID="3"; }
                 sda=new SqlDataAdapter("select Emp_Name,Emp_Birthday,Emp_Gender,Emp_Remark from Employee where Emp_DepID="+Convert.ToInt32(DepID),con);
                 ds=new DataSet();
                 sda.Fill(ds,"Employee");
                 this.dataGrid1.DataSource=ds.Tables["Employee"];
           }
 
添加信息窗体 (radioButton、comboBox控件):
1、添加: private void btnSubmit_Click(object sender, System.EventArgs e)
           {      int EmployeeID=Convert.ToInt32(this.textBox1.Text.Trim());
                       string Emp_Name=this.textBox2.Text.Trim();
                       string Emp_Birthday=this.textBox3.Text.Trim();
                       string Dep_Name=this.comboBox1.SelectedItem.ToString();
                       int Emp_DepID=0;
                       switch(Dep_Name)
                       {     case "技术部":      Emp_DepID=1;           break;
                            case "市场部":      Emp_DepID=2;           break;
                            case "人事部":      Emp_DepID=3;           break;           default:       break;
                       }
                       string Emp_Gender;
                       if(this.radioButton1.Checked)
                       {      Emp_Gender=this.radioButton1.Text;               }
                       else {      Emp_Gender=this.radioButton2.Text;               }
                       string Emp_Remark=this.textBox4.Text.Trim();
                       SqlConnection con=DB.createCon();
                       SqlCommand cmd=new SqlCommand("insert into Employee values('"+EmployeeID+"','"+Emp_Name+"','"+Convert.ToDateTime(Emp_Birthday)+"','"+Emp_DepID+"','"+Emp_Gender+"','"+Emp_Remark+"')",con);
                       cmd.Connection.Open();
                       cmd.ExecuteNonQuery();
                       cmd.Connection.Close();
                       MessageBox.Show("添加成功!");
                 }
           }
 
2、添加(用参数实现):
           private void btnSubmit_Click(object sender, System.EventArgs e)
           {           string Dep_Name=this.cmbDept.SelectedItem.ToString();
                       int Emp_DepID=0;
                       if(Dep_Name=="技术部")              {           Emp_DepID=1;      }
                       if(Dep_Name=="市场部")              {           Emp_DepID=2;      }
                       if(Dep_Name=="人事部")              {           Emp_DepID=3;           }
                       string Emp_Gender;
                       if(this.radioButton1.Checked)
                       {      Emp_Gender="男";             }
                       else
                       {      Emp_Gender="女";             }
                       SqlConnection con=DB.createCon();
                       SqlCommand cmd=new SqlCommand("insert into Employee(EmployeeID,Emp_Name,Emp_Birthday,Emp_DepID,Emp_Gender,Emp_Remark) values(@EmployeeID,@Emp_Name,@Emp_Birthday,@Emp_DepID,@Emp_Gender,@Emp_Remark)",con);
                       cmd.Parameters.Add(new SqlParameter("@EmployeeID",SqlDbType.Int,4));
                       cmd.Parameters["@EmployeeID"].Value=this.txtID.Text;
                       略--
                       cmd.Parameters.Add(new SqlParameter("@Emp_DepID",SqlDbType.Int,4));
                       cmd.Parameters["@Emp_DepID"].Value=Emp_DepID;
                       cmd.Parameters.Add(new SqlParameter("@Emp_Gender",SqlDbType.Char,10));
                       cmd.Parameters["@Emp_Gender"].Value=Emp_Gender;
                       cmd.Parameters.Add(new SqlParameter("@Emp_Remark",SqlDbType.VarChar,256));
                       cmd.Parameters["@Emp_Remark"].Value=this.txtRemark.Text;
                       cmd.Connection.Open();
                       cmd.ExecuteNonQuery();
                       cmd.Connection.Close();
                 }           }
 
娱乐信息管理系统
 
获取toolBar控件中的一个值,显示相应窗体:
           private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
           {      if(this.toolBar1.Buttons.IndexOf(e.Button)==0)
                 {      AllInfo a=new AllInfo();
                       a.MdiParent=this;
                       a.Show();
                 }
           }
 
删除选中的记录
           private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
           {      if(MessageBox.Show("你确定要删除这条记录吗?","用户您好",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation)==DialogResult.OK)
                 {      int i = this.dataGrid1.CurrentCell.RowNumber;
                       string str = this.dataGrid1[i,0].ToString();
con=DB.createCon();
cmd=new SqlCommand("delete musicTable where MusicID='"+str+"'",conn);
                       cmd.Connection.Open();
                       cmd.ExecuteNonQuery();
                       cmd.Connection.Close();
                       MessageBox.Show("删除成功!");
                       this.allTable();   //调用方法,重新填充
                 }
           }
 
更新:   private void button3_Click(object sender, System.EventArgs e) //更新
           {      string str="update musicTable set MusicName='"+textBox2.Text.Trim()+"',AuthorName='"+textBox3.Text.Trim()+"',PublishTime='"+textBox4.Text.Trim()+"' where MusicID=1";
                 cmd=new SqlCommand(str,con);
                 cmd.Connection.Open();
                 cmd.ExecuteNonQuery();
                 cmd.Connection.Close();
                 MessageBox.Show("更新成功!");
           }
 
用文件框绑绑定数据,浏览信息
private void MusicType_Load(object sender, System.EventArgs e)
           {           con=DB.createCon();
                       da=new SqlDataAdapter("select * from musicTable where FormatID=1",con);
                       ds=new DataSet();
                       da.Fill(ds,"musicTable");
                       this.txtID.DataBindings.Add("Text",ds,"musicTable.MusicID");
                       this.txtName.DataBindings.Add("Text",ds,"musicTable.MusicName");
                       this.txtPerson.DataBindings.Add("Text",ds,"musicTable.AuthorName");
                       this.txtTime.DataBindings.Add("Text",ds,"musicTable.PublishTime");
                       con.Close();
           }
 
用组合框绑绑定一列数据:
                 comboBox1.DataSource = ds.Tables["Login"];
                 comboBox1.DisplayMember = "Username";
浏览记录:
后移按钮:      this.BindingContext[ds,"musicTable"].Position=this.BindingContext[ds,"musicTable"].Position+1;
    ------=0为移到最前
移到最后:
      this.BindingContext[ds,"musicTable"].Position=this.ds.Tables["musicTable"].Rows.Count-1;
 
查询: 1、  string sql="select title,type,price,notes,pubdate from titles where title like '%"+strtitle+"%'";
    2、 //查询
           private void btnQuery_Click(object sender, System.EventArgs e)  
           {      string c="";        //定义搜索字段
                 if(this.txtName.Text=="")   //如果此文本框为空,则显示所有数据,可省略此段
                 {      c="CustomerName like '%'";          }
                 else
                 {      c+="CustomerName like '%"+this.txtName.Text+"%'";         }
                 if(this.txtAddress.Text!="")
                 {      c+=" and Address like '%"+this.txtAddress.Text+"%'";      }
                 if(this.DropDownList1.SelectedValue.ToString()!="") //DropDownList的查询字段
                 {      int CustomerTypeID=0;
                       string CustomerTypeName=this.DropDownList1.SelectedValue.ToString();                       
                       if(CustomerTypeName=="高级会员")
                       {      CustomerTypeID=1;            
                            c+=" and CustomerTypeName='高级会员'";
                       }         
                 }
                 con=DB.createCon();
                  sda=new SqlDataAdapter();
                 sda.SelectCommand=new SqlCommand("select CustomerID,CustomerName,Address,WebsiteName,Website,CustomerTypeName from CustomerList AS a JOIN DIM_CustomerType AS b ON a.CustomerTypeID=b.CustomerTypeID",con);
                 ds=new DataSet();
                 sda.Fill(ds,"CustomerList");
                 DataView dv=new DataView(ds.Tables["CustomerList"]);
                 dv.RowFilter=c;
                 this.DataGrid1.DataSource=dv;
                 this.DataGrid1.DataBind();    } 

你可能感兴趣的:(winForm)