ACCESS通訊錄

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.OleDb;

namespace 我的通訊錄
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataBinding();
        }
        private BindingManagerBase myBind;
        private void ClearTest()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
        private void DataBinding()
        {
            this.oleDbDataAdapter1.Fill(this.dataSet1,"information");
            this.textBox1.DataBindings.Add("Text",this.dataSet1,"information.姓名");
            this.textBox2.DataBindings.Add("Text", this.dataSet1, "information.年齡");
            this.textBox3.DataBindings.Add("Text", this.dataSet1, "information.電話");
            this.textBox4.DataBindings.Add("Text", this.dataSet1, "information.電子郵件");
            myBind=this.BindingContext[this.dataSet1,"information"];
        }
      

        private void button1_Click(object sender, EventArgs e)
        {
            this.myBind.Position = 0;
        }

        private void oleDbDataAdapter1_RowUpdated(object sender, OleDbRowUpdatedEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(this.myBind.Position==0)
            {
                MessageBox.Show("沒有前一個記錄了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information);                                    
            }
            else
            {
                this.myBind.Position--;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.myBind.Position == this.myBind.Count - 1)
            {
                MessageBox.Show("沒有後一條記錄了", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                this.myBind.Position++;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.ClearTest();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBox1.Text != "" & this.textBox2.Text != "" & this.textBox3.Text != "" & this.textBox4.Text != "")
                {
                    string insert = " INSERT INTO information( 姓名,年齡,電話,電子郵件) VALUES( '" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "')";
                    OleDbCommand comm = new OleDbCommand();
                    comm.CommandText = insert;
                    comm.Connection = this.oleDbConnection1;

                    this.oleDbConnection1.Open();
                    comm.ExecuteNonQuery();
                    this.oleDbConnection1.Close();
                    this.dataSet1.Tables["information"].Rows[this.myBind.Position].BeginEdit();
                    this.dataSet1.Tables["information"].Rows[this.myBind.Position].EndEdit();
                    this.dataSet1.AcceptChanges();

                    MessageBox.Show("添加記錄成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    MessageBox.Show("請將字段信息輸入完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (Exception error)
            {
                MessageBox.Show("添加記錄發生錯誤:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                if (this.oleDbConnection1.State == ConnectionState.Open)
                {
                    this.oleDbConnection1.Close();
                }
            }

        }

        private void button8_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("是否刪除記錄"," 提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK)
                try
                {

                    string delete = "DELETE From information WHERE 姓名='"+ this.textBox1.Text +"'";

                    OleDbCommand comm = new OleDbCommand();
                    comm.CommandText = delete;
                    comm.Connection = this.oleDbConnection1;

                    this.oleDbConnection1.Open();
                    comm.ExecuteNonQuery();
                    this.oleDbConnection1.Close();

                    this.dataSet1.Tables["information"].Rows[this.myBind.Position].Delete();
                    this.dataSet1.AcceptChanges();

                    MessageBox.Show(" 刪除記錄成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            catch(Exception error)
                {
                    MessageBox.Show("刪除記錄發生錯誤:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    if (this.oleDbConnection1.State == ConnectionState.Open)
                    {
                        this.oleDbConnection1.Close();
                    }


                }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBox1.Text != "" & this.textBox2.Text != "" & this.textBox3.Text != "" & this.textBox4.Text != "")
                {
                    string ModiString;
                    ModiString = "Update information Set 姓名='" + textBox1.Text + "'";
                    ModiString = ModiString + ",年齡='" + textBox2.Text + "'";
                    ModiString = ModiString + ",電話='" + textBox3.Text + "'";
                    ModiString = ModiString + ",電子郵件='" + textBox4.Text + "'";
                    OleDbCommand comm = new OleDbCommand();
                    comm.CommandText = ModiString + "where 姓名='" + textBox1.Text + "'";
                    comm.Connection = this.oleDbConnection1;

                    this.oleDbConnection1.Open();
                    comm.ExecuteNonQuery();
                    this.oleDbConnection1.Close();

                    this.dataSet1.Tables["information"].Rows[this.myBind.Position].BeginEdit();
                    this.dataSet1.Tables["information"].Rows[this.myBind.Position].EndEdit();
                    this.dataSet1.AcceptChanges();

                    MessageBox.Show("修改記錄成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.oleDbDataAdapter1.Fill(this.dataSet1, "information");
                }
                else
                {
                    MessageBox.Show("請將字段信息輸入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

                }

            }
            catch(Exception error)
            {
                MessageBox.Show("修改記錄發生錯誤:"+error.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                if (this.oleDbConnection1.State == ConnectionState.Open)
                {
                    this.oleDbConnection1.Close();
                }
              
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.myBind.Position == this.myBind.Count-1)
            {
                MessageBox.Show("沒有後一條記錄了", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                this.myBind.Position++;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章