c#

FORM1


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //数据库连接
            MySqlConnection conn = new MySqlConnection("Database=teach;server=localhost;User Id=root;Password=");
            
            //sql
            string sql = "select * from t_student where 1=1";
            if (this.code.Text != "") {
                sql += " and stu_code=" + this.code.Text.Trim();
            }
            if (this.name.Text != "") {
                sql += "  and stu_name like "+"\'"+this.name.Text.Trim()+"%\'";
            }
            if (this.age.Text != "") {
                sql += "  and age=" + this.age.Text.Trim();
            }

            //if (this.name.Text != "" || this.code.Text != "") {
                //sql += " where stu_code=" + this.code.Text.Trim() + " and  stu_name like " + "\'" + this.name.Text.Trim() + "%\'";
            
           // }
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            view.DataSource = ds.Tables[0];



        }

        private void button2_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("Database=teach;server=localhost;User Id=root;Password=");
            string sql = "delete from t_student where 1=1";
            if (this.code.Text != "")
            {
                sql += " and stu_code=" + this.code.Text.Trim();
            }
            if (this.name.Text != "")
            {
                sql += "  and stu_name like " + "\'" + this.name.Text.Trim() + "%\'";
            }
            if (this.age.Text != "")
            {
                sql += "  and age=" + this.age.Text.Trim();
            }
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            MessageBox.Show("删除成功");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        
        }

        private void age_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }
    }
}

  

FORM2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("Database=teach;server=localhost;User Id=root;Password=");
            conn.Open();
           
            string sql = "insert into t_student(stu_id,stu_code,stu_name,stu_sex,stu_dept,age)";
            string id = this.id.Text.Trim();
            string code=this.code.Text.Trim();
            string name=this.name.Text.Trim();
            string sex=this.sex.Text.Trim();
            string dept=this.dept.Text.Trim();
            string age=this.age.Text.Trim();
            if (this.code.Text != "" || this.name.Text != "" || this.sex.Text != "" || this.dept.Text != "" || this.age.Text != "") { 
               sql+="values("+id+","+code+","+"'"+name+"'"+","+"'"+sex+"'"+","+"'"+dept+"'"+","+age+")";
            }
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            //DataSet d = new DataSet();
            //ad.Fill(d);
            int i = cmd.ExecuteNonQuery();
            if (i == 1)
            {
                MessageBox.Show("添加成功");
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("Database=teach;server=localhost;User Id=root;Password=");
            conn.Open();
            string sql = "update t_student set ";
            string id = this.id.Text.Trim();
            string code = this.code.Text.Trim();
            string name = this.name.Text.Trim();
            string sex = this.sex.Text.Trim();
            string dept = this.dept.Text.Trim();
            string age = this.age.Text.Trim();
            if (this.code.Text != "" || this.name.Text != "" || this.sex.Text != "" || this.dept.Text != "" || this.age.Text != "")
            {
                sql += " stu_code=" + code + "," + "stu_name=" + "'" + name + "'" + "," + "stu_sex=" + "'" + sex + "'" + "," + "stu_dept=" + "'" + "dept" + "'" + "," + "age=" + age + " where stu_id=" + id;
            }
            MySqlCommand cmd=new MySqlCommand(sql,conn);
            int i=cmd.ExecuteNonQuery();
            if(i>=1){
            MessageBox.Show("success");
            }else{
            MessageBox.Show("eror");
            }
        }
    }
}

  

posted @ 2016-03-31 11:14  尘梦  阅读(156)  评论(0编辑  收藏  举报