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

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

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            SqlConnection conn = new SqlConnection("Server=tcp:qoau6i9shz.database.windows.net;Database=kukky;User ID=kukisan093;Password=ji#g$x9$ej#;Trusted_Connection=False;Encrypt=True;");
            conn.Open();
            SqlCommand s_com = new SqlCommand();
            s_com.CommandText = "select * from customer"; 
            s_com.Connection = conn;
            SqlDataReader s_read = s_com.ExecuteReader();
            while (s_read.Read()) // 迴圈會執行到沒有下一筆資料為止
            {
                //textbox.Text += s_read["欄位名稱"].ToString();
                //這裡只是舉例，會將每次讀到的row的特定欄位值接在textbox後面
                dataGridView1.Rows.Add(s_read["C_Name"], s_read["CAddress"]);

            }

            s_read.Close();
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=tcp:qoau6i9shz.database.windows.net;Database=kukky;User ID=kukisan093;Password=ji#g$x9$ej#;Trusted_Connection=False;Encrypt=True;");
            conn.Open();
            SqlCommand s_com = new SqlCommand();
            s_com.CommandText = "Insert into customer values ('" + textBox1.Text + "','" + textBox2.Text + "')";
            s_com.Connection = conn;
            s_com.ExecuteNonQuery();
            MessageBox.Show("資料新增完成");
        }
    }
}
