2011年6月5日 星期日

BeginTransaction與Commit和Rollback

參考微軟官網
---
引用本篇,主要的作用在:BeginTransaction與Commit和Rollback
POS.ERP等運用在拋轉.轉單.過帳.結轉.結案,運用此方法會較為妥善!
但須思考:近.遠程和效率與頻寬等等的環境因素
---
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()

Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction

' Start a local transaction
transaction = connection.BeginTransaction("SampleTransaction")

' Must assign both transaction object and connection
' to Command object for a pending local transaction.
command.Connection = connection
command.Transaction = transaction

Try
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"

command.ExecuteNonQuery()

' Attempt to commit the transaction.
transaction.Commit()
Console.WriteLine("Both records are written to database.")

Catch ex As Exception
Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
Console.WriteLine(" Message: {0}", ex.Message)

' Attempt to roll back the transaction.
Try
transaction.Rollback()

Catch ex2 As Exception
' This catch block will handle any errors that may have occurred
' on the server that would cause the rollback to fail, such as
' a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType())
Console.WriteLine(" Message: {0}", ex2.Message)
End Try
End Try
End Using
End Sub

Enterprise Library 詳細教學網

Enterprise Library 2.0
---
真是利害!!

Enterprise Library 教學網址參考

參考1
Microsoft Enterprise Library 5.0 系列 - 小聪崽的一切 - 博客园
參考2
EntLib - 博客园
參考3
Enterprise Library系列文章回顾与总结 - TerryLee - 博客园
參考4
[24] EntLib - 龙王 - 博客园
參考5(很受用唷)
Enterprise Library 2.0 -- Data Access Application Block - Daniel Pang - 博客园

Enterprise Library 如何使用外部配置文件

網路上這篇真的不多,翻遍所有;就是零散不集中,讓想要用 Enterprise Library 真是有點害怕!
主要是文件過少,大家放出來的經驗值也不高!
底下依此篇Enterprise Library 2.0 技巧 如何使用外部配置文件取精要和重點,只要如此作即可達到"外部配置文件 .config"
---------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

static void Main(string[] args)
{
try
{
FileConfigurationSource dataSource = new FileConfigurationSource("data-filesource.config");

DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(dataSource);

Database db = dbFactory.Create("EntLibInstance");

db.ExecuteNonQuery("ProcName");

}
catch (Exception ex)
{
FileConfigurationSource exceptionsSource = new FileConfigurationSource("exceptions-filesource.config");

ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);

ExceptionPolicyImpl exceptionPolicy = exceptionFactory.Create("Event Policy");

if (exceptionPolicy.HandleException(ex))

throw;
}
}
---
以上就祝大家好運,可以開始研究了

Read/Write App.config

請參考來源
--
可要慢慢吸收唷,不錯!!
輔助參考另一篇FileConfigurationSource

以上2篇可花了我1整天才找到,Enterprise Library 實務範例真是不多!!

2011年6月2日 星期四

SQL - 使用 PIVOT

參考來源
--
此範例限:SQL 2005/SQL 2008

Code_1:

Select * From dbo.銷售業績 Order By 年份, 月份

SELECT
年份,
[01] as '一月', [02] as '二月', [03] as '三月', [04] as '四月',
[05] as '五月', [06] as '六月', [07] as '七月', [08] as '八月',
[09] as '九月', [10] as '十月', [11] as '十一月', [12] as '十二月'
FROM (
SELECT 年份, 月份, 銷售量
FROM dbo.銷售業績
GROUP BY 年份, 月份, 銷售量
) as GroupTable
PIVOT
(
Sum(銷售量)
FOR 月份 IN ([01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12])
) AS PivotTable


Code_2:

Select * From dbo.[銷售業績By季] Order By 年份, 季, 月份

SELECT
年份,
[Q1] as '第一季', [Q2] as '第二季', [Q3] as '第三季', [Q4] as '第四季'
FROM (
SELECT 年份, 季, SUM(銷售量) as '季加總'
FROM dbo.[銷售業績By季]
GROUP BY 年份, 季
) as GroupTable
PIVOT
(
Sum(季加總)
FOR 季 IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable

MS-SQL 修改欄位

參考來源
--
ALTER TABLE Table1 ALTER Column T_Column_1 nvarchar(50) NOT NULL

實際:(須注意大小寫,必須和sql一樣)
ALTER TABLE BOOKS ALTER Column note1 varchar(20) NULL