1. Visual Studio 2010 T4 Text Templates - 文字範本與程式碼產生
2. Visual Studio 2010 SqlExector - 快速執行.sql檔案
現在我將兩個工具進行了一些整合,並將整合後的結果更能應用在實際的程式開發上,以下就是這整個概念的說明
1. 在SQL Server上建立如下的資料庫與包含的資料表
2. 在Visual Studio 2010中,建立一個空白的Web Application(C#)專案,並建立如下圖的資料夾結構
3. 編輯專案中的Web.Config檔案,加入你需要連線的資料庫連結字串
4. 接下來會使用到的功能就是這次整合好的成果.
5. 滑鼠右鍵選取SQL這個資料夾並選擇新增項目,在視窗中可看到一個新的分類"Likol",選擇"Sql Text Template"項目,並修改檔案名稱為"ProductCategory.tt".
6. 完成後會出現資料表的選擇畫面,先選擇稍早鎖建立的資料庫連線設定,並選擇"ProductCategory"這個資料表
7. 重複5的步驟,但這次選擇的新增項目改為"Data Text Template",並一樣選擇"ProductCategory"這個資料表
8. 重複5,6,7步驟,但資料表的部份選擇"Product"
9.上述步驟完成後可看到如下圖的畫面
10. 接下來我們在SQL這個資料夾使用右鍵選擇,並選取SqlExector來執行由Text Template所產生的SQL Script



11. 執行完成後,我們可以在資料庫中看到,相關資料異動的Store Procedure都已經建立完成.
12. 現在,我們就可以使用由"Data Text Template"所產生的相關資料表操作,來進行程式的撰寫.我建立了如下的程式界面與程式
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td colspan="2"><b>Create</b></td>
</tr>
<tr>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td><asp:Button ID="btnCreate" Text="Create" runat="server"
onclick="btnCreate_Click" /></td>
</tr>
</table>
<br/>
<br/>
<table>
<tr>
<td colspan="2"><b>Select</b></td>
</tr>
<tr>
<td><asp:TextBox ID="txtID" runat="server" /></td>
<td><asp:Button ID="btnSelect" Text="Select" runat="server"
onclick="btnSelect_Click" /></td>
</tr>
<tr>
<td colspan="2">
Name: <asp:Label ID="lblName" Font-Bold="true" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
namespace WebApplication1
{
public partial class ProductCategory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCreate_Click(object sender, EventArgs e)
{
Data.ProductCategory productCategory = new Data.ProductCategory();
productCategory.ID = Guid.NewGuid();
productCategory.Name = this.txtName.Text;
Data.ProductCategory.Create(productCategory);
}
protected void btnSelect_Click(object sender, EventArgs e)
{
Data.ProductCategory productCategory = new Data.ProductCategory();
productCategory.ID = new Guid(this.txtID.Text);
productCategory = Data.ProductCategory.Select(productCategory);
this.lblName.Text = productCategory.Name;
}
}
}
13. 輸入要新增的ProductCategroy名稱,並選擇Create.
14. 從資料表中找出剛剛新增的資料ID,並填入畫面,接著按下Select按鈕,就會將該Category的名稱顯示出來.
Data\SQL\ProductCategory.tt 產生的內容如下:
IF EXISTS(SELECT * FROM sys.objects WHERE type='P' AND name='sp_ProductCategory_Create')
DROP PROCEDURE sp_ProductCategory_Create
GO
CREATE PROCEDURE sp_ProductCategory_Create
@ID AS uniqueidentifier,
@Name AS nvarchar(50)
AS
BEGIN
INSERT INTO ProductCategory
(
ID,
Name
)
VALUES
(
@ID,
@Name
)
END
GO
IF EXISTS(SELECT * FROM sys.objects WHERE type='P' AND name='sp_ProductCategory_Update')
DROP PROCEDURE sp_ProductCategory_Update
GO
CREATE PROCEDURE sp_ProductCategory_Update
@ID AS uniqueidentifier,
@Name AS nvarchar(50)
AS
BEGIN
UPDATE ProductCategory SET
Name=@Name
WHERE
ID=@ID
END
GO
IF EXISTS(SELECT * FROM sys.objects WHERE type='P' AND name='sp_ProductCategory_Delete')
DROP PROCEDURE sp_ProductCategory_Delete
GO
CREATE PROCEDURE sp_ProductCategory_Delete
@ID AS uniqueidentifier
AS
BEGIN
DELETE ProductCategory WHERE
ID=@ID
END
GO
IF EXISTS(SELECT * FROM sys.objects WHERE type='P' AND name='sp_ProductCategory_Select')
DROP PROCEDURE sp_ProductCategory_Select
GO
CREATE PROCEDURE sp_ProductCategory_Select
@ID AS uniqueidentifier
AS
BEGIN
SELECT * FROM ProductCategory WHERE
ID=@ID
END
GO
Data\ProductCategory.tt 產生的內容如下:
//------------------------------------------------------------------------------
//
// This code was generated by Likol.VisualStudio.Template Tool.
// Last generater time: 2011/7/10 上午 06:23:11
//
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Likol.Data;
using Likol.Web.Validation;
namespace WebApplication1.Data
{
public partial class ProductCategory
{
private Guid _ID;
private string _Name;
public Guid ID
{
get { return this._ID; }
set { this._ID = value; }
}
[Length(50)]
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
public static void Create(ProductCategory _ProductCategory)
{
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter("ID", _ProductCategory.ID),
new SqlParameter("Name", _ProductCategory.Name)
};
string commandText = "sp_ProductCategory_Create";
DatabaseManager.ExecuteNonQuery(commandText, parameters);
}
public static void Update(ProductCategory _ProductCategory)
{
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter("ID", _ProductCategory.ID),
new SqlParameter("Name", _ProductCategory.Name)
};
string commandText = "sp_ProductCategory_Update";
DatabaseManager.ExecuteNonQuery(commandText, parameters);
}
public static void Delete(ProductCategory _ProductCategory)
{
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter("ID", _ProductCategory.ID)
};
string commandText = "sp_ProductCategory_Delete";
DatabaseManager.ExecuteNonQuery(commandText, parameters);
}
public static ProductCategory Select(ProductCategory _ProductCategory)
{
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter("ID", _ProductCategory.ID)
};
string commandText = "sp_ProductCategory_Select";
SqlConnection sqlConnection = null;
SqlDataReader sqlDataReader = DatabaseManager.ExecuteReader(commandText, parameters, ref sqlConnection);
bool hasData = sqlDataReader.Read();
if (!hasData) return null;
ProductCategory __ProductCategory = new ProductCategory();
__ProductCategory.ID = (Guid)sqlDataReader["ID"];
__ProductCategory.Name = (string)sqlDataReader["Name"];
sqlDataReader.Close();
sqlConnection.Close();
return __ProductCategory;
}
public static string GetLookupText(object propertyValue)
{
ProductCategory _ProductCategory = new ProductCategory();
_ProductCategory.ID = (Guid)propertyValue;
_ProductCategory = ProductCategory.Select(_ProductCategory);
if (_ProductCategory != null) return _ProductCategory.Name;
return "";
}
}
}
用T4 Text Templates的好處其實在於產生出來的格式雖然是固定的了,還是可以透過一些程式設計的方式進行異動,而且可以根據當時的需求進行修改,完全還是在程式設計的範疇之中.
0 意見:
張貼意見