C#的應用

C# 的窗口項目建立sql

內容管理爲醫院信息管理,基礎包括增刪改查,增刪改的時候注意要使用  try {    }  catch{   }模塊數據庫

鏈接函數函數

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

 

點擊選中DGVspa

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(); }

模塊更新code

//更新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(); }

查詢內容orm

//查詢病人信息
        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); }

添加server

//添加病人
        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(); } }

修改blog

//修改病人
        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); } }

刪除cmd

//刪除小科室
        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); } }
相關文章
相關標籤/搜索