2015年12月25日 星期五

資料庫,新增,刪除


資料庫期末



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.IO;//**

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();

        OleDbDataAdapter dAdapter;
        OleDbCommandBuilder cBuilder;
        DataTable dTable = new DataTable();
        BindingSource bSource;
        private string ID;
        public Form1()
        {
            InitializeComponent();
            connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\student\Desktop\WindowsFormsApplication7\bin\Debug\test1.mdb");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dAdapter = new OleDbDataAdapter("select * from person where 識別碼  ", connection);

            cBuilder = new OleDbCommandBuilder(dAdapter);
            dAdapter.Fill(dTable);

            bSource = new BindingSource();
            bSource.DataSource = dTable;

            dataGridView1.DataSource = bSource;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();
            dTable.Clear();
            OleDbCommand command2 = new OleDbCommand();
            command2.Connection = connection;

            command2.CommandText = "insert into person (name,stu_no,tel,sex) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

            command2.ExecuteNonQuery();

            dAdapter.Fill(dTable);

            connection.Close();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {

                try
                {
                    var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;

                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;

                    string query = "select* from person where name='" + Value.ToString() + "'";
                    command.CommandText = query;


                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {

                        ID = reader["識別碼"].ToString();
                        textBox1.Text = reader["name"].ToString();
                        textBox2.Text = reader["stu_no"].ToString();
                        textBox3.Text = reader["tel"].ToString();
                        textBox4.Text = reader["sex"].ToString();


                    }
                    connection.Close();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("ERROR" + ex);
                }
            }

            else if (e.ColumnIndex == 1)
            {
                if (MessageBox.Show("確定刪除此筆資料?", "刪除資料", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        var Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                        Value = dataGridView1.Rows[e.RowIndex].Cells["name"].Value;

                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;



                        command.CommandText = "delete from person WHERE name = '" + Value.ToString() + "'";

                        command.ExecuteNonQuery();
                        dTable.Clear();
                        dAdapter.Fill(dTable);

                        connection.Close();
                        MessageBox.Show("刪除成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ERROR" + ex);
                    }

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dTable.Clear();

            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;




            command.CommandText = "UPDATE person SET name = '" + textBox1.Text + "'WHERE 識別碼 = " + ID;

            command.ExecuteNonQuery();

            dAdapter.Fill(dTable);
            connection.Close();
        }
    }
}

2015年12月18日 星期五

    public partial class Form1 : Form
    {
        int a = 0, rnd1 = 0, b = 0;
       

        public Form1()
        {
            InitializeComponent();
            label1.Text = "電腦";
            label2.Text = "玩家1";
            textBox1.DataBindings.Add("Text", bindingSource1, "name", true);
            textBox2.DataBindings.Add("Text", bindingSource1, "sex", true);
            textBox3.DataBindings.Add("Text", bindingSource1, "stu_no", true);
            textBox5.DataBindings.Add("Text", bindingSource1, "秒數", true);
            textBox4.DataBindings.Add("Text", bindingSource1, "輸贏", true);

        }

        private void label1_Click(object sender, EventArgs e)
        {
           
        }

        private void button7_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            Random rnd = new Random();
            rnd1 = rnd.Next(1, 4);
            button2.BackColor = System.Drawing.Color.FromName("White");
            button3.BackColor = System.Drawing.Color.FromName("White");
            button1.BackColor = System.Drawing.Color.FromName("White");
            textBox4.Text = "";
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            rnd1 = rnd.Next(1, 4);
            b = b + 1;
            textBox5.Text = b.ToString();
           
         
        }

        private void button4_Click(object sender, EventArgs e)
        {
            a = 1;
            if (rnd1 == 1)
            {
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button1.BackColor = System.Drawing.Color.FromName("White");
            }

            if (rnd1 == 2)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("White");
            }
            if (rnd1 == 3)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("White");
            }
            timer1.Enabled = false;
            if ( rnd1 ==3)
            {
                textBox4.Text = "You WIN";
            }
            if (a == rnd1)
            {
                textBox4.Text = "平手唷";
            }
            if ( rnd1 ==2 )
            {
                textBox4.Text = "Loser";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            a = 2;
            if (rnd1 == 1)
            {
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button1.BackColor = System.Drawing.Color.FromName("White");
            }

            if (rnd1 == 2)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("White");
            }
            if (rnd1 == 3)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("White");
            }
            timer1.Enabled = false;
            if (a > rnd1 )
            {
                textBox4.Text = "You WIN";
            }
            if (a == rnd1)
            {
                textBox4.Text = "平手唷";
            }
            if (a < rnd1 || (a == 3 && rnd1 == 1))
            {
                textBox4.Text = "Loser";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            a = 3;
            if (rnd1 == 1)
            {
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button1.BackColor = System.Drawing.Color.FromName("White");
            }

            if (rnd1 == 2)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("White");
            }
            if (rnd1 == 3)
            {
                button1.BackColor = System.Drawing.Color.FromName("Black");
                button2.BackColor = System.Drawing.Color.FromName("Black");
                button3.BackColor = System.Drawing.Color.FromName("White");
            }
            timer1.Enabled = false;
            if (rnd1==2 )
            {
                textBox4.Text = "You WIN";
            }
            if (a == rnd1)
            {
                textBox4.Text = "平手唷";
            }
            if (rnd1==1 )
            {
                textBox4.Text = "Loser";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db2DataSet.猜拳1' 資料表。您可以視需要進行移動或移除。
            this.猜拳1TableAdapter.Fill(this.db2DataSet.猜拳1);
            // TODO: 這行程式碼會將資料載入 'db2DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter1.Fill(this.db2DataSet.person);
            // TODO: 這行程式碼會將資料載入 'db2DataSet.猜拳' 資料表。您可以視需要進行移動或移除。
            this.猜拳TableAdapter2.Fill(this.db2DataSet.猜拳);
            // TODO: 這行程式碼會將資料載入 '_db1__1_DataSet1.猜拳' 資料表。您可以視需要進行移動或移除。
            this.猜拳TableAdapter1.Fill(this._db1__1_DataSet1.猜拳);
            // TODO: 這行程式碼會將資料載入 '_db1__1_DataSet.猜拳' 資料表。您可以視需要進行移動或移除。
            this.猜拳TableAdapter.Fill(this._db1__1_DataSet.猜拳);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable2' 資料表。您可以視需要進行移動或移除。
            this.dataTable2TableAdapter.Fill(this.db1DataSet.DataTable2);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.DataTable1' 資料表。您可以視需要進行移動或移除。
            this.dataTable1TableAdapter.Fill(this.db1DataSet.DataTable1);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.record' 資料表。您可以視需要進行移動或移除。
            this.recordTableAdapter.Fill(this.db1DataSet.record);
            // TODO: 這行程式碼會將資料載入 'db1DataSet.person' 資料表。您可以視需要進行移動或移除。
            this.personTableAdapter.Fill(this.db1DataSet.person);
        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveFirst();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            bindingSource1.MovePrevious();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveNext();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveLast();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.EndEdit();
                personTableAdapter.Update(db1DataSet);

                MessageBox.Show("資料更新成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}


2015年11月20日 星期五

資料庫

office Access 2013
打開範例搜尋北風→複製客戶清單到EXCEL→重開Access另存新檔成2000年的mdb檔→檔案存在C槽


Visual Studio
工具箱→ bindingSource1→DataSoure→選擇檔案→DataMenber→選擇資料庫
工具箱→dataGridView1→選擇資料來源→bindingSource1

2015年11月6日 星期五

簡易計算基

   public partial class Form1 : Form
    {

        String aa;
        String m;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1 = sender as Button;
            textBox1.Text += button1.Text;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            button10 = sender as Button;
            textBox1.Text += button10.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2 = sender as Button;
            textBox1.Text += button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3 = sender as Button;
            textBox1.Text += button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4 = sender as Button;
            textBox1.Text += button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button5 = sender as Button;
            textBox1.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            button6 = sender as Button;
            textBox1.Text += button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            button7 = sender as Button;
            textBox1.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            button8 = sender as Button;
            textBox1.Text += button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            button9 = sender as Button;
            textBox1.Text += button9.Text;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (aa == null)
                return;
            Double a = Double.Parse(aa);
            Double b = float.Parse(textBox1.Text);
            Double d = float.Parse(textBox1.Text);
            Double c = 0;
            switch (m)
            {
                case "+":
                    c = a + b;
                    break;
                case "-":
                    c = a - b;
                    break;
                case "*":
                    c = a * b;
                    break;
                case "/":
                    c = a / d;
                    break;
               
            }
           
            textBox1.Text = c.ToString();
            if (d == 0) textBox1.Text = "除數不為零";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            button12 = sender as Button;
            m = button12.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            button13 = sender as Button;
            m = button13.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            button14 = sender as Button;
            m = button14.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            button15 = sender as Button;
            m = button15.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            button17 = sender as Button;
            m = button17.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
            Double a = Double.Parse(aa);
            Double c = 0;
            c = Math.Sqrt(a);
            textBox1.Text = c.ToString();


        }

        private void button18_Click(object sender, EventArgs e)
        {
            button18 = sender as Button;
            m = button18.Text;
            aa = textBox1.Text;
            textBox1.Text = "";
            Double a = Double.Parse(aa);
            Double c = 0;
            c =1/a;
            textBox1.Text = c.ToString();
            if (a == 0) textBox1.Text = "除數不為零";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            button20 = sender as Button;
            textBox1.Text += button20.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {

            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
         
        }
    }

2015年11月5日 星期四

計算機(+-*/)

    public partial class Form1 : Form
    {
        int x, y,c=0;
        float a, b,d;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x = Convert.ToInt32(textBox1.Text);
            x = int.Parse(textBox1.Text);
            y = Convert.ToInt32(textBox2.Text);
            y = int.Parse(textBox2.Text);
            c = x + y;
            label2 .Text=c.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            x = Convert.ToInt32(textBox1.Text);
            x = int.Parse(textBox1.Text);
            y = Convert.ToInt32(textBox2.Text);
            y = int.Parse(textBox2.Text);
            c = x - y;
            label2.Text = c.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            x = Convert.ToInt32(textBox1.Text);
            x = int.Parse(textBox1.Text);
            y = Convert.ToInt32(textBox2.Text);
            y = int.Parse(textBox2.Text);
            c = x * y;
            label2.Text = c.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            a = Convert.ToInt32(textBox1.Text);
            a = float.Parse(textBox1.Text);
            b = Convert.ToInt32(textBox2.Text);
            b = float.Parse(textBox2.Text);
            d = a / b;          
            label2.Text = d.ToString();
            if (b == 0) label2.Text = "除數不為零";
        }
    }
}

第二節課(螢幕保護程式)

 需要一個button,一個timer

   public partial class Form1 : Form
    {
        int x, y, a = 0, b = 0, c = 0, d = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            button1.Text = "";
         
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            x = a;
            if (a >= 650) b = 1;
            if (a <= 0) b = 0;
            if (b == 1)
            {
                a = a - 50;
            }
            else
            {
                a = a + 50;
            }
            y = c;
            if (c >= 300) d = 1;
            if (c <= 0) d = 0;
            if (d == 1)
            {
                c = c - 50;
            }
            else
            {
                c = c + 50;
            }
            button1.Location = new Point(x, y);        
        }
    }

第一節課 (按鈕移動)

需要一個button

    public partial class Form1 : Form
    {
        int x, y, c = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c = c + 50;
            x = c;
            y = 50;
            button1.Location = new Point(x, y);
            button1.Text = x.ToString();
        }
    }
}