using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DemoTwo
{
public partial class Form1 : Form
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlTransaction trans = null;
public Form1()
{
InitializeComponent();
conn = new SqlConnection();
conn.ConnectionString = "server=.;uid=sa;pwd=;database=HomeWork";
}
private void button1_Click(object sender, EventArgs e)
{
string s=null;
string s1=null;
if(radioButton1.Checked)
{
s=radioButton1.Text;
}
if(radioButton2.Checked)
{
s=radioButton2.Text;
}
if(radioButton3.Checked)
{
s1=radioButton3.Text;
}
else
{
s1=radioButton4.Text;
}
try
{
conn.Open();
trans = conn.BeginTransaction();
string sqlOne="insert into TableFour values('"+textBox9.Text+"','"+textBox10.Text+"')";
cmd = new SqlCommand(sqlOne, conn, trans);
cmd.ExecuteNonQuery();
string sqlTwo = "insert into TableThree values('" + textBox7.Text + "','" + textBox8.Text + "')";
cmd = new SqlCommand(sqlTwo, conn, trans);
cmd.ExecuteNonQuery();
string sqlThree = "insert into TableTwo values('" + textBox5.Text + "','" + textBox6.Text + "','" + s1 + "','" + textBox7.Text + "')";
cmd = new SqlCommand(sqlThree, conn, trans);
cmd.ExecuteNonQuery();
string sqlFour = "insert into TableOne values('" + textBox1.Text + "','" + textBox3.Text + "','"+int.Parse(textBox2.Text)+"','" + s + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox9.Text + "')";
cmd = new SqlCommand(sqlFour, conn, trans);
cmd.ExecuteNonQuery();
trans.Commit();
MessageBox.Show("添加成功!");
}
catch (SqlException ex)
{
trans.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
conn.Open();
trans = conn.BeginTransaction();
//1.删除表格一
string sqlOne="delete TableOne where BId='"+int.Parse(textBox1.Text)+"'";
cmd = new SqlCommand(sqlOne, conn, trans);
cmd.ExecuteNonQuery();
//2.根据BId查到医生的编号和病症的类型
string sqlFive = "select *from TableOne where BId='" + int.Parse(textBox1.Text) + "'";
cmd=new SqlCommand(sqlFive,conn,trans);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
string s=null;
if (sdr.HasRows)
{
for (int i = 0; i < sdr.FieldCount; i++)
{
s += sdr.GetValue(i) + " ";
}
}
string [] arr=s.Split(' ');
string YId=arr[4];
string TId=arr[6];
string sqlTwo = "delete TableTwo where Yid='"+int.Parse(YId)+"'";
cmd = new SqlCommand(sqlTwo, conn, trans);
cmd.ExecuteNonQuery();
string sqlSix="select *from TableTwo where YId='"+int.Parse(YId)+"'";
cmd = new SqlCommand(sqlSix, conn, trans);
SqlDataReader sqdr = cmd.ExecuteReader();
sqdr.Read();
sdr.Read();
string s1= null;
if (sdr.HasRows)
{
for (int i = 0; i < sdr.FieldCount; i++)
{
s1+= sdr.GetValue(i) + " ";
}
}
string[] arr1 = s1.Split(' ');
string KId=arr1[3];
string sqlThree = "delete TableThree where Kid='"+int.Parse(KId)+"'";
cmd = new SqlCommand(sqlThree, conn, trans);
cmd.ExecuteNonQuery();
string sqlFour = "delete TableFour where TId='"+int.Parse(TId)+"'";
cmd = new SqlCommand(sqlFour, conn, trans);
cmd.ExecuteNonQuery();
trans.Commit();
MessageBox.Show("删除成功!");
}
catch (SqlException ex)
{
trans.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}