Tuesday, June 23, 2009

Recursive Function

Question :
给出sum、min、max和n四个正整数,请输出所有将sum拆分为n个正整数之和,其中每个正整数k都满足:min <= k <= max。这n个正整数之间可以重复,不过由于加法交换率的作用,1 + 2和2 + 1便算是重复的拆分了。

  例如,sum = 5,n = 3,min = 1,max = 3,这时候满足条件的拆分方式只有两种:

* 1 + 1 + 3
* 1 + 2 + 2


My Answer:

class Program
{

static int[] array = null;
static void Main(string[] args)
{
Calculate(7, 3, 2, 3);

}

static void Calculate(int sum, int num, int min, int max)
{
if (array == null)
{
array = new int[num];
}

if (num == 1)
{
if (sum == min || sum == max)
{

if (sum == min)
array[0] = min;
if (sum == max)
array[0] = max;

PrintResult();
}

return;

}


for (int i = max; i >= min; i--)
{

array[num - 1] = i;
Calculate(sum - i, num - 1, min, i);

}



}




private static void PrintResult()
{
for (int i = 0; i < array.Length; i++)
{
if (i == array.Length-1)
{
Console.Write(array[i].ToString()+"\n");
}
else
{
Console.Write(array[i].ToString() + "+");
}
}


}

}

Monday, June 22, 2009

Webparts

文中大部分内容来自SharePoint Services 3.0 SDK

有两种方式:

一种方式是创建一个继承自System.Web.UI.WebControls.WebParts.WebPart的Web Part(SharePoint SDK中是推荐使用这种方式的),这个WebPart不仅可以在SharePoint Services中使用,也可以用于其他ASP.net程序中;

另一个方式是创建Windows SharePoint Services WebPart 类,也就是继承自Microsoft.SharePoint.WebPartPages.WebPart。这个WebPart只能在SharePoint Services中使用。



一、首先介绍第一种方式:创建一个基本的Web Part



Step 1:创建一个ASP.Net Web Part assembly.

启动Visual Studio 2005

创建一个c# class library项目,项目名称假定为HelloWorldWebPart。

添加引用System.Web。

在.cs文件中,将下面的代码复制过来。
using System;using System.Text;using System.Web.UI.WebControls.WebParts;namespace MyWebPartLibrary { public class HelloWorldWebPart : WebPart { protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.Write("Hello, World!"); } }}注意此处使用的是System.Web.UI.WebControls.WebParts

现在可以编译此解决方案,会生成一个dll文件。



Step 2:将生成的dll文件放到bin或GAC(global assembly cache, C:\WINDOWS\assembly)中



Bin目录是指SharePoint web application 跟目录下的bin目录,如:C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin。



Step 3:(可选)如果把dll放在bin目录下,设置安全属性(security attributes)



找到SharePoint web application根目录下的web.config文件,修改trust level属性,如



Step 4:将生成的Web Part添加到SafeControls列表中



打开SharePoint web application根目录下的web.config文件,在SafeControls中添加下面的语句:



注意此处,一定要仔细,我在测试时,把Assembly写成了Assemply,结果浪费了我几个小时去找错误。

关于如何查看PublickeyToken,可以网搜一下,最简单的方式是添加到GAC中,就可以直接查看了。

强命名可以直接右键单击工程,然后选择Signing标签,选中sign the assembly,然后新建一个强名称,如key.snk。



Step 5:创建.webpart文件



打开,其中myserver是SharePoint站点部署的服务器的名称。

此时你会看到MyWebPartLibrary.HelloWorldWebPart出现在列表中,在它前面的check box中打勾。

最后点击Populate Gallery,如果不出什么问题,Web Part应该已经添加到Team Site Gallery中。



Step 6:将Web Part添加到SharePoint站点中的页面上



导航到SharePoint站点

在要添加Web Part的页面中,点击Site Actions并选择Edit Page。

点击Add a Web Part,在随后出现的对话框中,点击底部的Advanced Web Part gallery and options。

打开Team Site Gallery,将HelloWordWebPart拖到页面上。
-

Wednesday, June 17, 2009

Regex in SQL Server 2005(ZT)

From http://yogeshyrbyogi.blogspot.com/2009/04/regular-expression-in-sqlserver-2005.html

Step 1 : enable OLE Automation object in SQL Server 2005

Step 2 : Create a function

Step 3: example :

select dbo.RegEx( N'xxx(111)0101', N'(^\w{5}[-]\w{5}[-][\w]*$)|(^\w{4}[(]\d{3}[)][\w-]*$)',0 )


SQL :


use[master]
GO
GRANT EXECUTE ON[sys].[sp_OASetProperty] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OAMethod] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OAGetErrorInfo] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OADestroy] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OAStop] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OACreate] TO[public]
GO
use[master]
GO
GRANT EXECUTE ON[sys].[sp_OAGetProperty] TO[public]
GO
sp_configure 'show advanced options',1
GO
reconfigure
go

exec sp_configure
go
exec sp_configure 'Ole Automation Procedures',1
-- Configuration option ‘Ole Automation Procedures’ changed from 0 to 1. Run the RECONFIGURE statement to install.
go
reconfigure
go



create FUNCTION dbo.RegEx
(
--Expression Target / Source
@Target varchar(5000),
--Regular Expression Pattern
@Pattern varchar(Max),
--whether the expression case sensitive
@CaseSensitive bit = 0
)
RETURNS bit
AS
BEGIN
DECLARE @ReturnOACreate int
DECLARE @ObjToken int
DECLARE @objMatches int
DECLARE @objMatch int
DECLARE @count int
DECLARE @results bit

EXEC @ReturnOACreate = sp_OACreate 'VBScript.RegExp', @ObjToken OUTPUT

--objecttoken OUTPUT
--Is the returned object token, and must be a local variable of data type int.
--This object token identifies the created OLE object and is used in calls to the other
--OLE Automation stored procedures.
--Return
--0 (success) or a nonzero number (failure) that is the integer value of the
--HRESULT returned by the OLE Automation object

IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END

EXEC @ReturnOACreate = sp_OASetProperty @ObjToken, 'Pattern', @Pattern

IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END

EXEC @ReturnOACreate = sp_OASetProperty @ObjToken, 'Global', false
IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END

EXEC @ReturnOACreate = sp_OASetProperty @ObjToken, 'IgnoreCase', @CaseSensitive
IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END

EXEC @ReturnOACreate = sp_OAMethod @ObjToken, 'Test', @results OUTPUT, @Target
IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END

EXEC @ReturnOACreate = sp_OADestroy @ObjToken
IF @ReturnOACreate <> 0 BEGIN
SET @results = 0
RETURN @results
END
--return 1 for success
RETURN @results
END

Wednesday, June 10, 2009

WebClient & Proxy Example

Example of using webclient and proxy to get web page

static void Main(string[] args)
{

GetWebPageWithProxy("proxy", "8080","dayang.sun", "toyota#2");



}

private static void GetWebPageWithProxy(string proxyname,string proxyport, string username,string pwd)
{
// create a string with proxy name and open port to make a webproxy
string proxyinfo = string.Format("http://{0}:{1}", proxyname, proxyport);
WebProxy myProxy = new WebProxy(proxyinfo, true);
//username and password for proxy
myProxy.Credentials = new NetworkCredential(username,pwd);



WebClient webClient = new WebClient();
webClient.Proxy = myProxy;
webClient.Encoding = Encoding.UTF8;

Uri loginPageUri = new Uri("http://www.google.com");
string html = webClient.DownloadString(loginPageUri);
}