C#的应用

C# 的窗口项目创建

内容管理为医院信息管理,基础包括增删改查,增删改的时候注意要使用  try {    }  catch{   }模块

连接函数

        SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS;database=0313;integrated security=sspi");

 

点击选中DGV

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            label2.Text = dataGridView2.SelectedCells[0].Value.ToString().Trim();
            textBox2.Text = dataGridView2.SelectedCells[1].Value.ToString().Trim();
            dateTimePicker1.Value = DateTime.Parse(dataGridView2.SelectedCells[2].Value.ToString());
            comboBox3.Text = dataGridView2.SelectedCells[3].Value.ToString().Trim();
        }

模块更新

        //更新DGV
        void initGDV(string sql, DataGridView gdv)
        {
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            gdv.DataSource = ds.Tables[0];
            con.Close();
        }
        //下拉菜单的更新
        void initCMB(string sql,ComboBox cmb )
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            cmb.Items.Clear();
            while (sdr.Read())
                cmb.Items.Add(sdr.GetValue(0).ToString());
            con.Close();
        }

查询内容

 //查询病人信息
        private void button1_Click(object sender, EventArgs e)
        {
            if(!checkBox1.Checked&&!checkBox2.Checked&&!checkBox3.Checked)
            {
                MessageBox.Show("请选择要查询的信息!");
                return;
            }
            string sql = "select d.dname as 大科室名, g.gname as 小科室名 ,p.pname as  病人姓名 from" +
                " department as d,pgroup as g,patient as p ";
            sql += "where g.dno=d.dno and p.gno=g.gno  ";
            if(checkBox1.Checked)
            {
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("请选择大科室名!");
                    return;
                }
                else
                    sql += "and d.dname='" + comboBox1.Text + "' ";
            }
            if (checkBox2.Checked)
            {
                if (comboBox2.Text == "")
                {
                    MessageBox.Show("请选择小科室名!");
                    return;
                }
                else
                    sql += "and g.gname='" + comboBox2.Text + "' ";
            }
            if (checkBox3.Checked)
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("请输入病人姓名!");
                    return;
                }
                else
                    sql += "and p.pname like '%" + textBox1.Text + "%' ";
            }
            sql += "order by 大科室名 desc,小科室名 ,病人姓名 desc ";
                initGDV(sql, dataGridView1);
        }

添加

//添加病人
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
            initGDV("select pno as 身份证号 ,pname as 病人姓名 ,pdate as 出生日期 ,gno as 小科室编号 from patient ", dataGridView2);
        }
//窗口2 的内容,连接数据库和添加更新模块       
 private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text==""||textBox2.Text==""||comboBox1.Text=="")
            {
                MessageBox.Show("请输入要添加的信息!");
                return;
            }
            if(textBox1.Text=="0")
            {
                MessageBox.Show("身份证号不能为0!");
                return;
            }
            try
            {
                con.Open();
                string sql = "insert into patient values(@pno,@pname,@pdate,@gno)";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.Add(new SqlParameter("@pno", textBox1.Text));
                cmd.Parameters.Add(new SqlParameter("@pname", textBox2.Text));
                cmd.Parameters.Add(new SqlParameter("@pdate", dateTimePicker1.Value));
                cmd.Parameters.Add(new SqlParameter("@gno", comboBox1.Text));
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                    MessageBox.Show("添加成功!");
            }
            catch
            {
                MessageBox.Show("添加失败,病人身份证号重复或小科室人数已到80!!");
            }
            finally { con.Close(); }
        }

修改

//修改病人
        private void button3_Click(object sender, EventArgs e)
        {
            if (label2.Text=="0"||textBox2.Text == "" || comboBox3.Text == "")
            {
                MessageBox.Show("请选择和填写好要修改的病人信息!");
                return;
            }
            try
            {
                con.Open();
                string sql = "update patient set pname =@pname,pdate=@pdate,gno=@gno where pno=@pno";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.Add(new SqlParameter("@pno", label2.Text));
                cmd.Parameters.Add(new SqlParameter("@pname", textBox2.Text));
                cmd.Parameters.Add(new SqlParameter("@pdate", dateTimePicker1.Value));
                cmd.Parameters.Add(new SqlParameter("@gno", comboBox3.Text));
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                    MessageBox.Show("修改成功!");
            }
            catch
            {
                MessageBox.Show("修改失败!或小科室人数已到80!!");
            }
            finally
            {
                label2.Text = "0";textBox2.Text = "";
                con.Close();
                initGDV("select pno as 身份证号 ,pname as 病人姓名 ,pdate as 出生日期 ,gno as 小科室编号 from patient ", dataGridView2);
            }
        }

删除

        //删除小科室
        private void button7_Click(object sender, EventArgs e)
        {
            if (label7.Text == "0")
            {
                MessageBox.Show("请选择要删除的小科室!");
                return;
            }
            DialogResult result=( MessageBox.Show("将删除对应科室的所有病人!", "警告!!!", MessageBoxButtons.YesNo));
            if (result == DialogResult.Yes)
            {
                try
                {
                    con.Open();
                    string sql = "delete from pgroup where gno=@gno ";
                    SqlCommand cmd = new SqlCommand(sql, con);
                    cmd.Parameters.Add(new SqlParameter("@gno", label7.Text));
                    int i = cmd.ExecuteNonQuery();
                    if (i > 0)
                        MessageBox.Show("删除成功!");
                }
                catch
                {
                    MessageBox.Show("删除失败!");
                }
                finally
                {
                    label7.Text = "0"; textBox3.Text = "";
                    con.Close();
                    initCMB("select gname from pgroup ", comboBox2);
                    initCMB("select gno from pgroup ", comboBox3);
                    initCMB("select distinct gmonth from pgroup", comboBox5);
                    initGDV("select gno as 小科室编号,gname as 小科室名 ,dno as 所属大科室编号,gmonth as 统计月份,gnum as 病人人数 from pgroup ", dataGridView4);
                }
            }

 

posted @ 2019-03-20 17:37  lincoding`  阅读(1323)  评论(0编辑  收藏  举报