增删改

来源:互联网 发布:淘宝店铺从哪里推广 编辑:程序博客网 时间:2024/06/11 09:50

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace xxw
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["xxw.Properties.Settings.TTConnectionString"].ToString();
        private string sqlQuery;
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private SqlCommandBuilder builder;
        private DataSet ds;
        private DataTable userTable;    


        private void SetDataObjects()
        {
            connection = new SqlConnection(connectionString);
            command = new SqlCommand(sqlQuery, connection);
            adapter = new SqlDataAdapter(command);
            builder = new SqlCommandBuilder(adapter);
            ds = new DataSet("MainDataSet");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData("SELECT * FROM SystemLog");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                adapter.Update(userTable);
                LoadData("SELECT * FROM SystemLog");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                connection.Close();
            }
        }

        private void LoadData(string sqlStr)
        {
            if (userTable != null)
            {
                userTable.Clear();
            }
            userDataGridView.DataSource = null;
            userDataGridView.Rows.Clear();
            userDataGridView.Refresh();
            sqlQuery = sqlStr;
            SetDataObjects();
            connection.Open();
            adapter.Fill(ds, "SystemLog");
            userTable = ds.Tables["SystemLog"];
            userDataGridView.DataSource = userTable.DefaultView;
            connection.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("要删除选择数据吗?",
                "删除提示", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
                == DialogResult.Yes)
            {
                try
                {
                    connection.Open();
                    int cnt = userDataGridView.SelectedRows.Count;
                    for (int i = 0; i < cnt; i++)
                    {
                        if (this.userDataGridView.SelectedRows.Count > 0 &&
                            this.userDataGridView.SelectedRows[0].Index !=
                            this.userDataGridView.Rows.Count - 1)
                        {
                            this.userDataGridView.Rows.RemoveAt(
                               this.userDataGridView.SelectedRows[0].Index);
                        }
                    }
                    adapter.Update(userTable);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
}