2008年6月19日

[KB][PLINQ]Parallel LINQ介紹(1)

隨著多核心的CPU逐漸成為主流,為了要充分利用CPU的效能,在去年大約七月的時候,微軟發佈了Microsoft Parallel Extensions to Framework 3.5,目前的版本為June 2008 CTP版本。其中的PLINQ是大家要注意的地方。

PLINQ可能是下一代.NET Framework 4.0 的內建功能(Parallel FX Library, PFX)之一,它包含了Parallel LINQ (PLINQ) and Task Parallel Library (TPL),再搭配F#,下一代能帶給程式設計師提升效能及減少程式複雜的好特性實在不少。

NextDotNet
從它的字義上,我們可以知道它是用來做平行處理。針對哪部分做處理呢?主要是針對LINQ to Objects以及LINQ to XML作並行的處理。它提供了並行處理的類別以及方法,讓你過去操作thread以及同步所需處理的複雜程式碼簡化。類別及方法在System.Threading這個組件中實作。

接下來,我以一個簡單的範例來作說明:

1. 首先,開啟一個Console專案,然後在專案中引用System.Threading 這個dll 組件。

2. 我們輸入下列的程式碼:


string[] words = new[] { "Hello", "World", "PLINQ", "IS", "Very", "NOT", "BAD", "!!", "Test", "GOOD" };
var MyQuery = from word in words select word;

foreach (string str in MyQuery)
{
Console.Write(str + '\n');
}

Console.Read();

然後,根據處理器循序處理的特性,我們可以看到輸出的結果的順序如下(如同在陣列中的順序):

seq

3. 變更成PLINQ的寫法:



string[] words = new[] { "Hello", "World", "PLINQ", "IS", "Very", "NOT", "BAD", "!!", "Test", "GOOD" };

var MyQuery = from word in words.AsParallel() select word;

MyQuery.ForAll(word => { Console.WriteLine(word); });

Console.Read();

然後我們去查看"單一個CPU"的呈現情形,看來是跟原來循序處理一樣。
seq1

如果我們把這個執行檔放到一個擁有雙核CPU的電腦上,執行的結果如下圖:seq2

我們可以看到,透過平行處理,把這個陣列分成兩部分處理,一部分是NOT, Hello, World, PLINQ, IS, Very,另一部分則是BAD, !!, Test, GOOD。

如果你再運行一次,你會發現執行的順序變得不一樣了,因為在平行處理時,會切割成兩部分(如果是雙核心CPU)作處理,但不是每次順序都一樣。
seq3

4. 如果放在四核心的CPU上執行,執行的順序就更複雜了。(執行了三次,順序都不一樣)
image

image

image

隨著硬體的功能提升,如何充分利用硬體來加速軟體? 微軟打算在下一代Framework中,透過平行運算充分的利用到硬體作加速及效能的提升。

未來這個系列將會介紹PLINQ的一些相關特性。

注意:基於是CTP版本,微軟在PLINQ功能上隨時仍會作調整。

2008年6月3日

[KB]How to find dependency in SQL server 2005

如果你是一個DBA,常常會有清理資料庫內不需要的物件的需求。

但是你又怕砍掉這些物件(Column、Table、View及Store Procedure等)會讓其他依賴這物件的物件變得沒有作用。你可以透過下列語法來建立一個Store Procedure來識別物件的Dependency。



USE master
GO
CREATE PROCEDURE sp_FindDependencies
(
@ObjectName SYSNAME,
@ObjectType VARCHAR(5) = NULL
)
AS
BEGIN
DECLARE @ObjectID AS BIGINT

SELECT TOP(1) @ObjectID = object_id
FROM sys.objects
WHERE name = @ObjectName
AND type = ISNULL(@ObjectType, type)

SET NOCOUNT ON ;

WITH DependentObjectCTE (DependentObjectID, DependentObjectName, ReferencedObjectName, ReferencedObjectID)
AS
(
SELECT DISTINCT
sd.object_id,
OBJECT_NAME(sd.object_id),
ReferencedObject = OBJECT_NAME(sd.referenced_major_id),
ReferencedObjectID = sd.referenced_major_id
FROM
sys.sql_dependencies sd
JOIN sys.objects so ON sd.referenced_major_id = so.object_id
WHERE
sd.referenced_major_id = @ObjectID
UNION ALL
SELECT
sd.object_id,
OBJECT_NAME(sd.object_id),
OBJECT_NAME(referenced_major_id),
object_id
FROM
sys.sql_dependencies sd
JOIN DependentObjectCTE do ON sd.referenced_major_id = do.DependentObjectID
WHERE
sd.referenced_major_id <> sd.object_id
)
SELECT DISTINCT
DependentObjectName
FROM
DependentObjectCTE c
END

建立完成後,為了讓這個SP可以在任何一個資料庫中使用,請使用sp_ms_marksystemobject來將這個SP註冊為系統物件。

EXECUTE sp_ms_marksystemobject 'sp_FindDependecies'


接下來,您就可以透過它來找物件的相依性。

exec sp_FindDependencies 'employees'

result

2008年6月2日

[Info]你知道嗎?Hotfix可以從網路下載了.

現在,你可以直接從KB文章中下載所需要的Hotfix。

apply_hotfix

 

過去總是要聯絡微軟技術支援中心,透過技術支援工程師協助才能順利下載,現在你可以直接從網路上下載。但是要注意到語系(ENG 、CHT...)、OS平台(x86、x64以及IA)的相容。完成填表後,不久你就可以收到包含下載連結的email。

save_hotfix

若您下載的是SQL Server的hotfix,從SQL Server 2000開始,hotfix具有向前相容的特性(就是後面的hotfix會包含前面hotfix的內容),所以當您找不到某個語言或平台的hotfix時,可以找後面版本的hotfix來作升級。

 

如何識別 SQL Server 的版本

可供 SQL Server 2000 SP 4 中的 Hotfix累計清單

在 SQL Server 2005 Service Pack 2 所修正錯誤的清單

2008年5月28日

ASP.NET MVC Preview 3 Release

Scott Gu昨天在他的Blog發表了ASP.NET MVC Preview 3的內容。其中包含了一些更新還有新功能。有興趣的朋友可以去下列網址看看:

http://weblogs.asp.net/scottgu/archive/2008/05/27/asp-net-mvc-preview-3-release.aspx

 

下載 , ASP.NET MVC Preview 3 framework source code and framework unit tests

Windows 7 Demo

The demo from the All Things D conference today.

我覺得實在是太酷了, 要準備存錢買新NB了. Orz

2008年5月21日

[Info]Enterprise Library 4.0發佈了

NewpnpheroA

The patterns & practices Enterprise Library is a collection of application blocks designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source code that can be used "as is," extended, or modified by developers to use on enterprise development projects.

Enterprise Library 4.0是For Visual Studio 2008的版本。

你可以到這裡下載。

如果你在使用上有遇到一些問題,或是想找一些sample code以及教學,可以到codeplex討論群組找看看有沒有解決方法 - http://www.codeplex.com/entlib

[Info]Silverlight 2.0 教學網站

silverlight_learn

一個Silverlight教學的網站,內容很豐富。提供的是Step By Step的教學,可以邊看邊學,讚!!

有興趣的朋友可以去看看

http://www.miketaulty.com/SLVideos.html

2008年5月6日

如何檢查SQL Server連線問題?

今天在MSDN Blog上看到這篇列出檢查步驟的文章,節錄部分內容跟大家分享一下。

連線不通的原因

Basically, when you failed to connect to your SQL Server, the issue could be:

1) Network issue,

2) SQL Server configuration issue.

3) Firewall issue,

4) Client driver issue,

5) Application configuration issue.

6) Authentication and logon issue.

檢查步驟;

Step 1: Network issue

ping -a <your_target_machine>

(use -4 and -6 for IPv4 and IPv6 specifically)

ping -a <Your_remote_IPAddress>

nslookup (type your local and remote machine name and IP address multiple times)

If you are not able to ping your target machine, it has high chance that either the network is broken or the target machine is not running. It's possible the target machine is behind a firewall and the firewall blocks the packets sent by ping, though. Windows firewall does not block ping (ECHO) packet by default. The correctness of DNS configuration on the network is vital to SQL connection. Wrong DNS entry could cause of all sorts of connectivity issue later. See this link for example, "Cannot Generate SSPI Context" error message, Poisoned DNS.

Step 2: SQL Server configuration issue

You need to make sure the target SQL Server is running and is listening on appropriate protocols. You can use SQL Server Configuration Manager (SCM) to enable protocols on the server machine. SQL Server supports Shared Memory, Named Pipes, and TCP protocols (and VIA which needs special hardware and is rarely used). For remote connection, NP and/or TCP protocols must be enabled. Once you enabled protocols in SCM, please make sure restart the SQL Server.

You can open errorlog file to see if the server is successfully listening on any of the protocol. The location of errorlog file is usually under:

%ProgramFile%Microsoft SQL Server/MSSQLxx.xxx/MSSQL/Log

If the target SQL instance is a named instance, you also need to make sure SQL Browser is running on the target machine. If you are not able to access the remote SQL Server, please ask your admin to make sure all these happen.

Step 3: Firewall issue

A firewall on the SQL Server machine (or anywhere between client and server) could block SQL connection request. An easy way to isolate if this is a firewall issue is to turn off firewall for a short time if you can. Long term solution is to put exception for SQL Server and SQL Browser.

For NP protocol, please make sure file sharing is in firewall exception list. Both file sharing and NP use SMB protocol underneath.

For TCP protocol, you need put the TCP port on which the SQL Server listens on into exception.

For SQL Browser, please put UDP port 1434 into exception.

Meanwhile, you can put sqlservr.exe and sqlbrowser.exe into exception as well, but this is not recommended. IPSec between machines that we are not trusted could also block some packets. Note that firewall should never be an issue for local connections.

Step 4: Client driver issue

First try:

telnet <your_target_machine> <TCP_Port>

You should be able to telnet to the SQL server TCP port if TCP is enabled. Otherwise, go back to check steps 1-3. Then, use OSQL, SQLCMD, and SQL Management Studio to test sql connections. If you don't have those tools, please download SQL Express from Microsoft and you can get those tools for free.

Possilbe command use be:

osql -E -SYour_target_machine\Your_instance for Windows Auth

osql -Uyour_user -SYour_target_machine\Your_instance for SQL Auth

SQLCMD also applies here. In addition, you can use <“-Stcp:Your_target_machine, Tcp_port” for TCP, “-Snp:Your_target_machine\Your_instance” for NP, and “-Slpc:Your_target_machine\Your_instance” for Shared Memory. You would know if it fails for all protocols or just some specific procotols.

At this stage, you should not see general error message such as error 26 and error 40 anymore. If you are using NP and you still see error 40 (Named Pipes Provider: Could not open a connection to SQL Server), please try the following steps:

a) Open a file share on your server machine.

b) Run “net view file://your_target_machine/” and “net use file://your_target_machine/your_share” (You can try Map Network Drive from Windows Explorer as well)

If you get failure in b), it's very likely you have OS/Network configuration issue, which is not SQL Server specific. Please search on internet to resolve this issue first.

You can try connection using both Windows Authentication and SQL Authentication. If the tests with all tools failed, there is a good chance that steps 1-3 were not set correctly, unless the failure is logon-related then you can look at step 6.

If you succeeds with some of the tools, but fails with other tools, it's probably a driver issue. You can post a question on our forum and give us the details.

You can also use “\windows\system32\odbcad32.exe” (which ships with Windows) to test connection by adding new DSN for various drivers, but that's for ODBC only.

Step 5: Application issue

If you succeed with steps 1-4 but still see failure in your application, it's likely a configuration issue in your application. Think about couple of possible issues here.

a) Is your application running under the same account with the account you did tests in step 4? If not, you might want to try testing in step 4 under that account or change to a workable service account for your application if possible.

b) Which SQL driver does your app use?

c) What's your connection string? Is the connection string compatible to your driver? Please check http://www.connectionstrings.com/ for reference.

Step 6: Authentication and logon issue

This is probably the most difficult part for sql connectivity issues. It's often related to the configuration on your network, your OS and your SQL Server database. There is no simple solution for this, and we have to solve it case by case. There are already several blogs in sql_protocols talking about some special cases and you can check them see if any of them applies to your case. Apart from that, things to keep in mind:

a) If you use SQL auth, mixed authentication must be enabled. Check this page for reference http://msdn.microsoft.com/en-us/library/ms188670.aspx

b) Make sure your login account has access permission on the database you used during login ("Initial Catalog" in OLEDB).

c) Check the eventlog on your system see if there is more information

reference from:

http://blogs.msdn.com/sql_protocols/archive/2008/04/30/steps-to-troubleshoot-connectivity-issues.aspx

2008年4月15日

[好文推薦]ASP.NET 上傳檔案進度顯示

今天上班的時候,黃忠成老大傳給我一篇他剛Po好的文章。仔細一看,這個方法實在太棒了,有一種「黯然銷魂」的感覺。另一方面又覺得黃老大實在很大方,書都還沒推出就先把文章放出來,這種心胸實在很偉大  face7

 

不多說,有興趣的可以到黃老大那拜訪- http://blog.csdn.net/Code6421/archive/2008/04/15/2292821.aspx

 

順便推薦一下新書 (拿到看完後再來po心得)-

NE20274

http://www.drmaster.com.tw/info2.asp?no=NE20274

2008年4月9日

IIS7如何設定啟用ASP?

在Windows Server 2003的IIS6中,預設是停用ASP功能,在Vista以及Windows Server 2008內的IIS7也是一樣。

只要到[控制台]->[程式和功能]->[開啟或關閉Windows功能], 然後勾選下圖標示的設定:


就可以支援ASP了~