[Azure]Web應用程式+SQL佈署(一)地面環境準備

老婆最近換了新筆電,要幫她重新準備Demo環境還蠻瑣碎的! 安裝SQL Server、還原Demo資料庫、設定IIS功能,最後再從版控取得新版本程式碼才能上版。

這次換個方式,幫老婆申請好免費的Azure帳號,筆記漫步在雲端 A Walk in the Clouds ,把Demo環境從地面上到雲端。

 

拆成4篇筆記,讓老婆輕鬆動手做,這一篇先準備熟悉的地面環境(小型資料庫 + mvc網站 )。

(一)地面環境準備(資料庫、網站程式準備)

(二)Azure服務設定(Web應用程式+SQL)

(三)SQL資料庫透過SSMS佈署在Azure

(四)Web應用程式透過Visual Studio佈署在Azure

 

建立地面資料庫

1.建立一個地面上的資料庫、一個測試資料表及12筆撲克牌人物。

建立資料庫

CREATE DATABASE [GroundDB]
CONTAINMENT = NONE
ON  PRIMARY 
( NAME = N'GroundDB', FILENAME = N'E:\db\GroundDB.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
LOG ON 
( NAME = N'GroundDB_log', FILENAME = N'E:\db\GroundDB_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)
GO

 

測試資料表

CREATE TABLE [dbo].[POKERS](
	[ID] [int] NOT NULL,
	[NAME] [varchar](20) NULL,
	[TITLE] [varchar](10) NULL,
	[COLOR] [varchar](10) NULL,
 CONSTRAINT [PK_POKERS] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

 

12筆測試資料

INSERT INTO POKERS
VALUES(1, 'David',      'King', 'Spades' ),
      (2, 'Charlemagne','King', 'Hearts' ),
      (3, 'Caesar',     'King', 'Diamonds'),
      (4, 'Alexander',  'King', 'Clubs' ),
      (5, 'Athena',     'Queen','Spades' ),
      (6, 'Judith',     'Queen','Hearts' ),
      (7, 'Rachel',     'Queen','Diamonds' ),
      (8, 'Argine',     'Queen','Clubs' ),
      (9, 'Ogier',      'Jack', 'Spades'),
      (10,'La Hire',    'Jack', 'Hearts'),
      (11,'Hector',     'Jack', 'Diamonds' ),
      (12,'Hammer',     'Jack', 'Clubs' )

 

建立地面網站專案

1.建立一般ASP.NET Web應用程式

2.選取範本

3.專案建立成功後

 

網站程式碼準備

1.Models資料夾按右鍵>加入>類別 > Poker.cs

public class Poker
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Color { get; set; }
    }

 

2.安裝輕量聞名的ORM利器Dapper

專案右鍵>管理Nuget套件>搜尋Dapper>安裝

3.Controllers資料夾按右鍵>加入>控制器

輸入控制器名稱PokersController

輸入PokersController.cs程式碼

using Dapper;
using GroundWeb.Models;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Mvc;

namespace GroundWeb.Controllers
{
    public class PokersController : Controller
    {
        // GET: Pokers
        public ActionResult Index()
        {
            using (SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["GroundDBConnectionString"].ConnectionString))
            {
                db.Open();
                return View(db.Query<Poker>(" select * from pokers", null));
            }
        }
    }
}

 

4.Index方法上按右鍵>新增檢視

5.組態檔案Web.config新增連線字串

  <connectionStrings>
    <add name="GroundDBConnectionString" connectionString="Data Source=Stanley14\SQL2014;Initial Catalog=GroundDB;User ID=我是帳號;Password=我是密碼" providerName="System.Data.SqlClient"/>
  </connectionStrings>

 

執行地面網站程式: 

網址還在localhost,還在地面。

完成了(一)地面環境準備(網站程式、資料庫準備),接著(二)Azure服務設定(Web應用程式+SQL)

 

參考