2012年3月30日 星期五

Q & A : LocalDB IIS 權限問題

開發工具 : Visual Studio 11 Beta
作業系統 : Windows 7 Ultimate
IIS 7.5

因為blog有留言詢問有Local DB的網站,放在IIS上確無法執行的問題。米米想應該是權限問題吧,不過因為我用來測試的電腦上,居然沒有裝IIS (怎麼會有這種事??),所以沒有做測試,就請匿名的網友試試NTFS授權。
沒想到,回應是授權了也不work!!
這倒是一個有趣的問題,因此一早就把測試機器上的IIS 裝起來。
  • 使用Visual Studio 11 Beta 建一個Web Site在IIS
image
  • 加入LocalDB資料庫到網站
image
  • 新增Web Form
image
  • 將Table拉到網頁設計畫面
image
  • 一執行,果然出現了問題
image
  • 錯誤訊息如下,最後一行寫說”請看一下Event Log吧…”

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
)

  • 打開Event Viewer看一看,有兩個明確的錯誤訊息。一個說Profile沒有load
image

  • 一個是拒絕存取
image
  • 針對第一個問題,Profile沒有load。開啟C:\Windows\System32\inetsrv\config目錄下的applicationHost.config檔案,找一下ProcessModel的設定,將以下兩項都設為true。
image
  • 針對第2個問題,權限的地方。檢查一下IIS目前的設定,TestWebSite網站建立在DefaultAppPool下。因為DefaultAppPool在IIS 7.5 使用的是ApplicationPoolIdentity來執行程式
image
  • 利用檔案總管,找到資料庫的Database1_log.ldf檔,授權ApplicationPoolIdentity帳號有Full Control 權限
image
image
  • 選不到帳號,要自己輸入字串:
image
  • 設Full Control
image

  • 按相同步驟,利用檔案總管,找到資料庫的Database1_log.mdf檔,授權ApplicationPoolIdentity帳號有Full Control 權限

  • 測試前,先IISRESET,讓組態確定套用。
image
  • 再執行網頁,問題就解決了。
image

2012年3月19日 星期一

ASP.NET Web API

ASP.NET Web API是一個建立HTTP服務的架構 (framework),本文介紹如何撰寫一個簡單的ASP.NET Web API程式。

本文開發工具 : Visual Studio 11 Beta & .NET Framework 4.5

  • 建立一個ASP.NET MVC 4 Web Application專案

image

  • 選取Web API範本

image

  • 在Models目錄新增一個類別

image

  • 將類別命名為Employee

image

  • Employee程式碼
namespace MvcWebAPI.Models
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
  • 新增一個Controller

image

  • 設定名稱為EmployeesController

image

  • 程式

using MvcWebAPI.Models;

namespace MvcWebAPI.Controllers
{
    public class EmployeesController : ApiController
    {
        List<Employee> employees =  new List<Employee>{
                new Employee() {ID=1,Name="Mary",Age=30},
                new Employee() {ID=2,Name="Anne",Age=40},
                new Employee() {ID=3,Name="Cherry",Age=50},
                new Employee() {ID=4,Name="Candy",Age=34}
            };

        public IEnumerable<Employee> GetEmployees ( )
        {
            return employees;
        }

    }
}

  • 修改Index.cshtml,利用jQuery取回JSON格式的Employee清單

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>ASP.NET Web API</title>

     <script src="Scripts/jquery-1.6.2.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $.getJSON("api/employees",
                function (data) {
                    $('#emp').empty();
                    $.each(data, function (key, val) {
                        $('<li/>', { html: val.Name }).appendTo($('#emp'));
                    });
                });
    });
    </script>
   
</head><body>
      <div id="emp">

      </div>  
</body>
</html>

  • 按F5執行

image

2012年3月15日 星期四

Net Framework 4.5與Visual Studio 11- ASP.NET 4.5新功能(1)

 

原文刊於 .NET Magazine國際中文電子雜誌

此篇文章改寫原刊於 .NET Magazine的文章,原文是使用Visual Studio 11 Developer Preview版工具撰寫的,本文改用Visual Studio Beta工具測試,重新抓圖並修訂部分工具的差異。原文URL:

http://blogs.uuu.com.tw/Articles/post/2011/11/30/Net-Framework-45與Visual-Studio-11-ASPNET-45新功能(1).aspx

本文撰寫時使用的工具是Visual Studio 11 Beta,資料庫則為SQL Server 2012版,因此本文探討的內容在正式版上市時可能不適用。

ASP.NET 4.5版中包含許多新特性來開發網站應用程式,Visual Studio 11 Beta版開發工具也有許多改善,讓撰寫網頁的動作更為簡化,本篇文章將介紹一些開發工具提供的新特性。

 

開發工具新增功能

在Visual Studio 2010設計ASP.NET網頁時,有很多工作我們會透過「Smart Task」來完成,例如底下有一個Entity Data Source控制項,透過「Smart Task」可以設定它的資料來源,參考圖1所示。

clip_image002

圖 1:透過Smart Task設定控制項屬性。

2012年3月14日 星期三

ASP.NET 4.5 一使用到驗證控制項就出錯


本文使用Visual Studio 11 Beta工具
ASP.NET 4.5 Web Forms網頁一使用到驗證控制項就出錯,錯誤訊息如下:
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
  • 測試步驟為建立一個WebSite
image
  • 加一個網頁
image
  • 選Web Form
image
  • 加 Control

<asp:Label ID="Label1" runat="server" Text="Input Text"> </asp:Label>
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
          ControlToValidate="TextBox1" ForeColor="Red"> * </asp:RequiredFieldValidator>
      <asp:Button ID="Button1" runat="server" Text="Button" />

image
  • 按F5執行就會出錯,而且是摸不著頭緒的訊息:
image
請神孤狗不到…只好亂試
解法方法如下:
  • 加入一個Bin目錄
image
  • 在目錄中,放Microsoft.ScriptManager.jQuery.dll組件(補充:在Visual Studio 2012版本,需要在bin目錄下放入AspNet.ScriptManager.jQuery.dll組件。)
image
  • 總算可以正常執行
image

Attach DB到 LocalDB

雖然有點笨,但,總之可以將AdventureWorks與Northwind、Pubs利用Visual Studio 11 Beta的SQL Server Object Explorer掛到 LocalDB 下供我測試用了。

  • 新增一個Query

image

  • 下指令
USE master;
GO
CREATE DATABASE AdventureWorks
    ON (FILENAME = 'C:\Source\AdventureWorks_Data.mdf'),
    (FILENAME = 'C:\Source\AdventureWorks_Log.ldf')
    FOR ATTACH;
GO
  • 執行

image

  • 會自動升級資料庫的版本

image

  • 試掛 Northwind

image

  • Pubs

image

很好,比我想像的容易很多。

不過,有沒有更簡單的方式?

例如有一個網站需要DB

  • 先建立一個Web Site:

image

  • 將DB檔案加入專案

image

  • 雙擊檔案名稱,無法開啟,會得到錯誤訊息。

image

  • 參考此篇文章:

Visual Studio 11 Beta與SQL Server 2008 mdf檔案型資料庫

在Server Explorer刪掉再重建Connection就可以了。

2012年3月13日 星期二

.Net Framework 4.5與Visual Studio 11–資料存取工具

 

此篇文章改寫原刊於 .NET Magazine的文章,原文是使用Visual Studio 11 Developer Preview版工具撰寫的,本文改用Visual Studio 11 Beta工具測試,重新抓圖並修訂部分工具的差異。原文URL:

http://blogs.uuu.com.tw/Articles/post/2011/11/16/Net-Framework-45%E8%88%87Visual-Studio-11%E2%80%93%E8%B3%87%E6%96%99%E5%AD%98%E5%8F%96%E5%B7%A5%E5%85%B7.aspx

本文撰寫時使用的工具是Visual Studio 11 Beta,資料庫則為SQL Server 2012 Express版,因此本文探討的內容在正式版上市時可能不適用。

本文延續《.Net Framework 4.5與Visual Studio 11–工具與程式語言新功能》一文的內容來介紹Visual Studio 11 Beta中的SQL Server Object Explorer的使用。

 

資料存取工具

Visual Studio 11 Beta中的SQL Server Object Explorer可以讓你直接在此工具中連結、建立、刪除、管理SQL Server資料庫,也可以透過它來建立登入(login) 帳號等安全性的設定動作。

 

新增SQL Server

SQL Server Object Explorer工具可以用來連接到資料庫伺服器,並列出資料庫的相關物件。你可能會想到原來的「SQL Server Object Explorer」工具中包含一個「Data Connection」的節點,為何又要有一個SQL Server Object Explorer工具,這兩個項目在使用上有一點點不同。「Data Connection」項目通常是用來搭配Visual Studio 11 Beta設計應用程式來使用,而SQL Server Object Explorer工具則是用來管理資料庫伺服器,它的功能比較類似之前管理SQL Server 2008時所使用的「SQL Server Management Studio」工具。

.Net Framework 4.5與Visual Studio 11 –工具與程式語言新功能

 

原文刊於 .NET Magazine國際中文電子雜誌

此篇文章改寫原刊於 .NET Magazine的文章,原文是使用Visual Studio 11 Developer Preview版工具撰寫的,本文改用Visual Studio Beta工具測試,重新抓圖並修訂部分工具的差異。原文URL:

http://blogs.uuu.com.tw/Articles/post/2011/11/02/Net-Framework-45%E8%88%87Visual-Studio-11-%E2%80%93%E5%B7%A5%E5%85%B7%E8%88%87%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80%E6%96%B0%E5%8A%9F%E8%83%BD.aspx

本文撰寫時使用的工具是Visual Studio 11 Beta,資料庫則為SQL Server 2012 Express版,因此本文探討的內容在正式版上市時可能不適用。

 

Visual Studio 11 Beta開發工具

Visual Studio 2010與Visual Studio 11 Beta開發工具兩者可以並存在相同的電腦環境上。更好的是,專案檔案與方案檔案具備向下相容的能力。不過,參考圖1所示,目前在筆者的電腦中安裝了.NET Framework 4版,從Visual Studio 11 Beta專案的「Target Framework」設定來看,目前Visual Studio 11 Beta不僅能夠開發預設的.NET Framework 4.5版程式,也能夠開發前版.NET Framework2~.NET Framework 4的應用程式。

clip_image002

圖 1:Visual Studio 11 Beta專案的「Target Framework」設定。

2012年3月12日 星期一

Visual Studio 11 Beta Setup 專案

 

前一版VS2010發行時,就有耳聞未來不會有安裝setup專案,果不其然,傳言要屬實了嗎?在Visual Studio 11 Beta新增專案的視窗,已經找不到安裝專案的範本了。以後都要買InstallSheld了嗎??

image

2012年3月3日 星期六

Visual Studio 11 Beta LightSwitch初體驗

Visual Studio LightSwitch是微軟提供的一個開發工具,簡易開發流程,讓可以讓你快速地建立一個商用級應用程式。廢話不多說,直接看設計步驟來體驗一下這個工具。

開啟Visual Studuio 11 Beta,建立一個LightSwitch專案

image

定義應用程式所需資料

第一步要先定義應用程式所需資料,假設這是一個員工資料管理系統,利用Create new table建立資料表

image

直接在畫面上編輯Table名稱,和所需的屬性,例如Name:

image

員工可能有EMail欄位,Type部分可以直接選擇內建的型別,例如Email Address

image

Solution Explorer視窗中會列出此Data Source

image

建立使用者介面

下一步建立畫面,選取Add Screen

image

選取資料展示的樣版,預設有很多不同的樣版可以選擇,並設定Screen Data

image

這樣就大功告成了。

 

執行與測試

在Visual Studio 11 Beta工具中,按F5執行,就可以看到我們寫好的應用程式

image

你可以直接在程式中編輯資料,然後選擇上方的Save按鈕存檔

image

若資料輸入有誤,也會自動檢查,並顯示錯誤訊息

image

應用程式中編輯的資料會被存放在Bin\Data目錄下名為ApplicationDatabase.mdf檔案型資料庫檔案之中。

image

這是不是很簡單呢?

總網頁瀏覽量