用的是微软已经给封装好的sqlhelper.cs !!!

解决方案 »

  1.   

    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 医院管理系统代码
    {
        public partial class EmployeeSalariesManage : Form
        {
            SqlConnection conn;
            SqlDataAdapter sda;
            SqlCommandBuilder builder;
            DataSet ds;
            DataTable dt;
            DataRow row;
            float  shouldSalaries = 0;
            float rate = 0;
            float prize = 0;
            string connection = "Data Source=.;Initial Catalog=LifeLine;Integrated Security=True";
            public EmployeeSalariesManage()
            {
                InitializeComponent();
                      }
    //添加数据
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtEmployeType.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtShouldSalaries.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtRate.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtPrize.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtFactSalaries.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            }       
            private void Add_Click(object sender, EventArgs e)
            {
                try
                {
                    conn = new SqlConnection(connection);
                    sda = new SqlDataAdapter("select * from EmployeeSalaries", conn);
                    SqlCommandBuilder builder1 = new SqlCommandBuilder(sda);
                    DataTable table = new DataTable();
                    sda.Fill(table);
                    DataRow DR = table.NewRow();                DR[0] = txtEmployeeID.Text;
                    table.Rows.Add(DR);
                    sda.Update(table);
                    dataGridView1.DataSource = table;
                    MessageBox.Show("数据添加成功!");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
    //数据查询
            private void Query_Click(object sender, EventArgs e)
            {
                try
                {
                    string command = "";
                    if (comboBoxSelectCondition.Text == "")
                    {
                        MessageBox.Show("查询条件不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (comboBoxSelectCondition.Text == "员工代号")
                        {
                            command = "select * from dbo.EmployeeSalaries where 代号='" + txtEmployeeID.Text + "'";
                        }
                       if (txtEmployeeID.Text.Length == 0 && txtEmployeType.Text.Length  == 0 && txtShouldSalaries.Text.Length == 0 && txtRate.Text.Length == 0 && txtPrize.Text.Length  == 0 && txtFactSalaries.Text.Length==0)
                        {
                            MessageBox.Show("输入的内容不能为空!");
                        }
                        else
                        {
                            conn = new SqlConnection(connection);
                            sda = new SqlDataAdapter(command, conn);
                            builder = new SqlCommandBuilder(sda);
                            ds = new DataSet();
                            dt = new DataTable();
                            sda.Fill(dt);
                            if (int.Parse(dt.Rows.Count.ToString()) == 0)
                            {
                                MessageBox.Show("你输入的内容表中不存在!");
                            }
                            else
                            {
                                dataGridView1.DataSource = dt;
                                txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                                                     }
                        }
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
    //数据删除
            private void Delete_Click(object sender, EventArgs e)
            {
                try
                {
                    if (MessageBox.Show("你确定要删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        conn = new SqlConnection(connection);
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("delete from  dbo.EmployeeSalaries where 代号=" + txtEmployeeID.Text, conn);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        MessageBox.Show("删除成功!");
                        sdr.Close();
                        sda = new SqlDataAdapter("select * from dbo.EmployeeSalaries", conn);
                        builder = new SqlCommandBuilder(sda);
                        ds = new DataSet();
                        dt = new DataTable();
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                        txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();                    
                        dataGridView1.DataSource = dt;
                    }
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
    //数据修改
            private void Change_Click(object sender, EventArgs e)
            {
                try
                {
                    shouldSalaries = float.Parse(txtShouldSalaries.Text.ToString());
                    rate = float.Parse(txtRate.Text.ToString());
                    prize = float.Parse(txtPrize.Text.ToString());
                    txtFactSalaries.Text = Convert.ToString((shouldSalaries + prize)-shouldSalaries*rate);                conn = new SqlConnection(connection);
                    conn.Open();
                    SqlCommand cmd1 = new SqlCommand("update dbo.EmployeeSalaries set 应发工资=@ShouldSalaries where 代号=" + txtEmployeeID.Text, conn);
                    cmd1.Parameters.Add(new SqlParameter("@ShouldSalaries", txtShouldSalaries.Text));
                    SqlDataReader sdr1 = cmd1.ExecuteReader();
                    sdr1.Close();                SqlCommand cmd2 = new SqlCommand("update dbo.EmployeeSalaries set 纳税率=@Rate where 代号=" + txtEmployeeID.Text, conn);
                    cmd2.Parameters.Add(new SqlParameter("@Rate", txtRate.Text));
                    SqlDataReader sdr2 = cmd2.ExecuteReader();
                    sdr2.Close();               
                    sda = new SqlDataAdapter("select * from EmployeeSalaries", conn);
                    builder = new SqlCommandBuilder(sda);
                    ds = new DataSet();
                    dt = new DataTable();
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                    txtEmployeeID.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                   MessageBox.Show("数据修改成功!");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }
    }