//建立與mysql連線 MySqlConnection conn = new MySqlConnection("server=localhost;user id=帳號; password=密碼;database=資料庫名稱"); conn.Open(); MySqlCommand cmd = conn.CreateCommand(); string sql = "CREATE TABLE IF NOT EXISTS address_book (sid INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , name VARCHAR( 20 ) NOT NULL , phone VARCHAR( 20 ) NOT NULL , address VARCHAR( 20 ) NOT NULL) ENGINE = MYISAM ;"; cmd.Connection = conn; cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO address_book (name, phone, address ) VALUES('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "')"; cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT * FROM address_book"; MySqlDataReader mysql_datareader = cmd.ExecuteReader(); GridView1.DataSource = mysql_datareader; GridView1.DataBind(); conn.Close();