资源描述:
《增加、删除、修改 程序代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、增加、删除、修改程序代码privatevoidbutton1_Click(objectsender,EventArgse){//创建Connection对象stringstrcon=@"DataSource=LENOVO-87EFEB86SQLEXPRESS;InitialCatalog=companyinfo;IntegratedSecurity=True";SqlConnectioncon=newSqlConnection(strcon);//添加新数据的SQL语句stringsql="inser
2、tintoemployeevalues('"+this.textBox1.Text.Trim()+"','"+this.textBox2.Text.Trim()+"','"+Convert.ToDateTime(this.textBox3.Text.Trim())+"','"+Convert.ToDateTime(this.textBox4.Text.Trim())+"','"+this.textBox5.Text.Trim()+"',"+Convert.ToDecimal(this.textBox6.
3、Text.Trim())+")";////sql语句的另一种写法////stringsql=string.Format("insertintoemployeevalues('{0}','{1}',{2},'{3}')",this.txtName.Text,this.cboSex.Text,Convert.ToInt32(this.txAge.Text),this.cboClass.Text);//创建Command对象SqlCommandcomm=newSqlCommand(sql,con);try{/
4、/打开连接con.Open();//执行插入操作if(comm.ExecuteNonQuery()>0){MessageBox.Show("添加成功");//启用id的文本框//this.txtId.Enabled=true;//this.btnDelete.Enabled=true;//this.btnModify.Enabled=true;//this.txAge.Text="";//this.txtName.Text="";}elseMessageBox.Show("添加失败");}catch(E
5、xceptionex){MessageBox.Show(ex.Message);}finally{con.Close();//关闭连接}}privatevoidbutton2_Click(objectsender,EventArgse){//创建Connection对象stringstrcon=@"DataSource=LENOVO-87EFEB86SQLEXPRESS;InitialCatalog=companyinfo;IntegratedSecurity=True";SqlConnectionc
6、on=newSqlConnection(strcon);//添加新数据的SQL语句stringsql="deleteemployeewhere姓名='"+textBox1.Text.Trim()+"'";////sql语句的另一种写法////stringsql=string.Format("insertintoemployeevalues('{0}','{1}',{2},'{3}')",this.txtName.Text,this.cboSex.Text,Convert.ToInt32(this.txA
7、ge.Text),this.cboClass.Text);//创建Command对象SqlCommandcomm=newSqlCommand(sql,con);try{//打开连接con.Open();//执行插入操作if(comm.ExecuteNonQuery()>0){MessageBox.Show("添加成功");//启用id的文本框//this.txtId.Enabled=true;//this.btnDelete.Enabled=true;//this.btnModify.Enabled=t
8、rue;//this.txAge.Text="";//this.txtName.Text="";}elseMessageBox.Show("添加失败");}catch(Exceptionex){MessageBox.Show(ex.Message);}finally{con.Close();//关闭连接}}privatevoidbutton3_Click(objectsender,EventArgse){////创建Connection对象