C#Winfrom資料庫增刪改查執行個體--DataAdapter版

來源:互聯網
上載者:User

標籤:c# winfrom 增刪改查

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.OleDb;using System.Configuration;namespace TestDbOper2{    public partial class Form1 : Form    {        static string m_connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;                OleDbConnection m_conn;        OleDbCommand m_cmd;        OleDbDataAdapter m_da;        DataTable m_dt;        enum DbState        {            dsAdd,//增加狀態            dsMod,//修改狀態            dsDel,//刪除狀態            dsBro //瀏覽狀態          }        DbState m_DbState;               public Form1()        {            InitializeComponent();            m_conn = new OleDbConnection(m_connstr);            m_cmd = new OleDbCommand(" SELECT * FROM Users", m_conn);            m_da = new OleDbDataAdapter(m_cmd);            OleDbCommandBuilder cb = new OleDbCommandBuilder(m_da);            m_dt = new DataTable();            dataGridView1.DataSource = m_dt;        }        private void Form1_Load(object sender, EventArgs e)        {            dataGridView1.AutoGenerateColumns = false;            btnQuery_Click(sender, e);        }        //查詢        private void btnQuery_Click(object sender, EventArgs e)        {            m_DbState = DbState.dsBro;            SetBtnState();            m_cmd.CommandText = "select * from users where username like '%"+textBox2.Text+"%'";            m_dt.Clear();            m_da.Fill(m_dt);            m_conn.Close();          }        //增加        private void btnAdd_Click(object sender, EventArgs e)        {            m_DbState = DbState.dsAdd;            SetBtnState();            txtUserName.Text = "";            txtUserAge.Text = "";            txtUserSex.Text = "";        }        //修改        private void btnMod_Click(object sender, EventArgs e)        {            if (dataGridView1.CurrentCell.RowIndex >= m_dt.Rows.Count) { return; }            m_DbState = DbState.dsMod;            SetBtnState();            txtUserName.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["username"].ToString();            txtUserAge.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["userage"].ToString();            txtUserSex.Text = m_dt.Rows[dataGridView1.CurrentCell.RowIndex]["usersex"].ToString();        }        //刪除        private void btnDel_Click(object sender, EventArgs e)        {            if (MessageBox.Show("確定刪除嗎?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) { return; }            try            {                m_dt.Rows[dataGridView1.CurrentCell.RowIndex].Delete();                m_da.Update(m_dt.GetChanges());                m_dt.AcceptChanges();                MessageBox.Show("刪除成功");            }            catch(Exception ex)             {                MessageBox.Show("刪除失敗"+ex.Message);            }        }        //雙擊        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)        {            btnMod_Click(sender, e);        }        //儲存        private void btnSave_Click(object sender, EventArgs e)        {            if (m_DbState == DbState.dsMod)            {                try                {                    DataRow dr= m_dt.Rows[dataGridView1.CurrentCell.RowIndex];                    dr.BeginEdit();                    dr["username"] = txtUserName.Text;                    dr["userage"] = txtUserAge.Text;                    dr["usersex"] = txtUserSex.Text;                    dr.EndEdit();                    m_da.Update(m_dt.GetChanges());                    m_dt.AcceptChanges();                    MessageBox.Show("儲存成功");                }                catch (Exception ex)                {                    MessageBox.Show("儲存失敗 " + ex.Message);                }            }            else            {                try                {                    DataRow dr = m_dt.NewRow();                    dr["username"] = txtUserName.Text;                    dr["userage"] = txtUserAge.Text;                    dr["usersex"] = txtUserSex.Text;                    m_dt.Rows.Add(dr);                    m_da.Update(m_dt.GetChanges());                    m_dt.AcceptChanges();                    MessageBox.Show("增加成功");                }                catch (Exception ex)                {                    MessageBox.Show("增加失敗"+ex.Message);                }            }            m_DbState = DbState.dsBro;            SetBtnState();        }        private void btnCancel_Click(object sender, EventArgs e)        {            m_DbState = DbState.dsBro;            SetBtnState();        }        private void SetBtnState()        {            btnQuery.Enabled = m_DbState == DbState.dsBro;            btnAdd.Enabled = m_DbState == DbState.dsBro;            btnMod.Enabled = m_DbState == DbState.dsBro;            btnDel.Enabled = m_DbState == DbState.dsBro;            btnSave.Enabled = m_DbState != DbState.dsBro;            btnCancel.Enabled = m_DbState != DbState.dsBro;        }    }}

C#Winfrom資料庫增刪改查執行個體--DataAdapter版

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.