Friday, May 22, 2009

数据字典例子 转自csdn

create table datadict(tablename varchar(20),cloname varchar(20),colnote varchar(20),conshow int,consort int)
insert into datadict select 't_product_list','listid','产品标识',1,2
insert into datadict select 't_product_list','pname','产品名称',1,1
insert into datadict select 't_product_list','ptype','产品类型',0,3
insert into datadict select 't_product_list','psource','产品来源',0,4
insert into datadict select 't_user','userid','产品标识',1,1
insert into datadict select 't_user','username','用户名称',1,2
insert into datadict select 't_user','userage','用户年龄',1,3
insert into datadict select 't_user','user***','用户性别',0,4


create table t_product_list(listid int,pname varchar(20),ptype varchar(20),psource varchar(20))
insert into t_product_list select 1,'茶叶','日用品', '仓库一'
insert into t_product_list select 2,'鞋子', '日用品' , '仓库二'
insert into t_product_list select 3,'帽子', '日用品' , '仓库三'


declare @sql varchar(1000)
set @sql=''
select @sql=@sql+','+colnote+'='+cloname
from datadict where tablename='t_product_list' and conshow=1
set @sql='select '+stuff(@sql,1,1,'')+' from t_product_list'
exec(@sql)

--排序的
declare @tn varchar(50),@b int ,@e int,@sql varchar(1000)
select @tn = 't_product_list',@b= 1,@e = 2
select @sql = isnull(@sql+',','')+colnote+'='+cloname from datadict
where tablename = @tn and consort between @b and @e order by consort
set @sql = 'select '+@sql+' from '+@tn
exec(@sql)


English Version

create table datadict(tablename varchar(20),cloname varchar(20),colnote varchar(20),conshow int,consort int)
insert into datadict select 't_product_list','listid','ptag',1,2
insert into datadict select 't_product_list','pname','pname',1,1
insert into datadict select 't_product_list','ptype','ptype',0,3
insert into datadict select 't_product_list','psource','psource',0,4
insert into datadict select 't_user','userid','ptag',1,1
insert into datadict select 't_user','username','uname',1,2
insert into datadict select 't_user','userage','uage',1,3
insert into datadict select 't_user','user***','ugender',0,4


create table t_product_list(listid int,pname varchar(20),ptype varchar(20),psource varchar(20))
insert into t_product_list select 1,'tea','gro1', 'warehouse1'
insert into t_product_list select 2,'shoe', 'gro1' , 'warehouse2'
insert into t_product_list select 3,'hat', 'gro1' , 'warehouse3'



declare @sql varchar(1000)
set @sql=''
select @sql=@sql+','+colnote+'='+cloname from datadict where tablename='t_product_list' and conshow=1
--print @sql
set @sql='select '+stuff(@sql,1,1,'')+' from t_product_list'
print @sql
exec(@sql)

select ptag=listid,pname=pname from t_product_list
select * from t_product_list

--排序的
declare @tn varchar(50),@b int ,@e int,@sql varchar(1000)
select @tn = 't_product_list',@b= 1,@e = 2
select @sql = isnull(@sql+',','')+colnote+'='+cloname from datadict
where tablename = @tn and consort between @b and @e order by consort
set @sql = 'select '+@sql+' from '+@tn

print @sql
exec(@sql)

Thursday, May 21, 2009

(ZT)C#正则表达式

只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^\d{n}$"。
只能输入至少n位的数字:"^\d{n,}$"。
只能输入m~n位的数字:。"^\d{m,n}$"
只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。
只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。
只能输入非零的正整数:"^\+?[1-9][0-9]*$"。
只能输入非零的负整数:"^\-[1-9][]0-9"*$。
只能输入长度为3的字符:"^.{3}$"。
只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。
只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。
验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
验证是否含有^%&',;=?$\"等字符:"[^%&',;=?$\x22]+"。
只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"
验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。
验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。
验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。
验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。
验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。
利用正则表达式限制网页表单里的文本框输入内容:

用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF] /g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1

s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)

匹配双字节字符(包括汉字在内):[^\x00-\xff]

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正则表达式:\n[\s| ]*\r

匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的正则表达式:(^\s*)|(\s*$)

String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

利用正则表达式分解和转换IP地址:

下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:

function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:

var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
符号解释:

字符
描述

\
将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。

^
匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。

$
匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。

*
匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}。

+
匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。

?
匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。

{n}
n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。

{n,}
n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。

{n,m}
m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。

?
当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo",'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'。

.
匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。

(pattern)
匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '\(' 或 '\)'。

(?:pattern)
匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。

(?=pattern)
正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。

(?!pattern)
负向预查,在任何不匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始

x|y
匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。

[xyz]
字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。

[^xyz]
负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。

[a-z]
字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。

[^a-z]
负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符。

\b
匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。

\B
匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。

\cx
匹配由 x 指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 'c' 字符。

\d
匹配一个数字字符。等价于 [0-9]。

\D
匹配一个非数字字符。等价于 [^0-9]。

\f
匹配一个换页符。等价于 \x0c 和 \cL。

\n
匹配一个换行符。等价于 \x0a 和 \cJ。

\r
匹配一个回车符。等价于 \x0d 和 \cM。

\s
匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。

\S
匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。

\t
匹配一个制表符。等价于 \x09 和 \cI。

\v
匹配一个垂直制表符。等价于 \x0b 和 \cK。

\w
匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。

\W
匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。

\xn
匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,'\x41' 匹配 "A"。'\x041' 则等价于 '\x04' & "1"。正则表达式中可以使用 ASCII 编码。.

\num
匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。例如,'(.)\1' 匹配两个连续的相同字符。

\n
标识一个八进制转义值或一个向后引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为向后引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。

\nm
标识一个八进制转义值或一个向后引用。如果 \nm 之前至少有 nm 个获得子表达式,则 nm 为向后引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的向后引用。如果前面的条件都不满足,若 n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。

\nml
如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。

\un
匹配 n,其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如, \u00A9 匹配版权符号 (?)。

Wednesday, May 13, 2009

Why C# is Getting Functional? Cos for LINQ, OOP is choked! (ZT)

Functional Programming stuff are creeping in into C#. Some people like it, some people confused, some people discouraged.

Why do we need to add Functional Programming stuff into C#? Cos OOP principles get choked to do one thing C# would like to do, LINQ. Other ways are needed. And it’s Functional Programming way.

Implicitly Typed Local Variable

In C# 3.0, we have the implicitly typed local variable like this:

var i = 5;

So, we don’t need to define the type like this:

int i = 5;

Don’t get it wrong, this line of code will raise an error:

var i = 5;
i = “Booyah!”;

Why? Unlike the “var” you’ve heard from Dynamically Typed Languages, in C# 3.0, once a variable is typed you can’t change the type. In the example above, i’s type is inferred from the Right Hand Side and became an “int”. Once it is typed as an “int” it stays an “int”. You can’t change the type.

This capability (type inferrencing) comes from Functional Programming. Why it is needed in C#? Cos it’s handy to do Higher Order Function (function that takes or output functions as parameter) as we don’t need to define the type, let the expression body (code at Right Hand Side) infer the type.

Then, why do we need to do Higher Order Function kind of thing? So that we can pass around code (function) just like we pass around data. In other words, code/function becomes data! Code, just like data is an expression. Hmm, “Everything is an Expression”. Another Functional stuff. How C# does it? Thru “Lambda Expressions”.

Before we get into “Lambda Expressions” I’d like you tointroduce Extension Methods first.

Extension Methods

In C# 3.0, we can add a method to a type dynamically/at runtime. We can add method to a type not at compile time. We can add method to a type even if we don’t have the source code (class) of the type. How? Thru “Extension Methods”.

See the code below:

public static void MyFunkyMethod(this string s)
{
// whatever
}

Notice the keyword “this” in the argument part. See the “string” type next to it. Now System.String would have that “MyFunkyMethod” method.

All you need to do is import the namespace where the MyFunkyMethod resides, and you can do this:

String myString = “Booyah!”;
myString.MyFunkyMethod(); // See, MyFunkyMethod is added to System.String not at compile time of the System.String.

We can achieve such thing in OOP way thru Visitor Design Patterns. But it’s more cumbersome off course. Anyway, why do we need to add “Extension Methods” capability to C#? Cos we want to add methods to existing types in C#. Why do we need to do that? Read on!

Lambda Expressions

Remember my statement above? That C# will use “Lambda Expressions” to be able to pass around code (as expression) just like data? Actually Lambda Expression is evolution of Delegate & Anonymous Type. Here’s one example:

s => {return s+1}

that can be simplified as

s => s+1

This expression says, it takes “s” as input and the body part says it returns “s+1”. I hope you can do the delegate or anonymous type of that expression. It’s trivial. My point is, yes, you can do it in delegate or anonymous type way, but “Lambda Expressions” is more neat.

LINQ, putting it all together

Now, I will put all the pieces together.

With “Extension Methods”, I can add a method let say named “Where” to types that are Collection, but not in compile time. With Lambda Expression, I can pass code as argument to that method. And in Lambda Expressions, I don’t need to define the type as it is inferred.

So, I might have a code like this:

Where(s => s.Length == 5);

It takes an expression “s => s.Length == 5” as its argument. With Extension Methods capability, I can add this “Where” method to array, collections, List, etc.

I also can do other method named “Select” that is also receive a Lambda Expression as argument and I may have code like this:

Select(s => s);

Lambda Expression “s => s” is “s => {return s}”, simply returns the argument.

Now, see this code:

string[] names = {“John Petrucci”, “David”, “John Myung”, “Mike Portnoy”, “Normy”);
IEnumerable expr = names.Where(s => s.Length == 5).Select(s => s);

Or you can do it in more aesthetic way:

IEnumerable expr = names
.Where(s => s.Length == 5)
.Select(s => s);

This expression “names.Where(s => s.Length == 5).Select(s => s);” is actually a query expression. Can you see it? It querys element of names (string) that has length equals to 5. It’s a query expression, but it’s strongly typed, and Intellisense can be used too. Not like a TSQL string “Select * from names where len(s)=’5’” that is a string. You can’t check the query syntax correctness at compile time. But in expression “names.Where(s => s.Length == 5).Select(s => s);” you can!

Then it’s up to us now to do the actual query in the “Where” and “Select” method. We can write code that simply query collections in memory, and also… yes, databases! Just like ORMs! Not just that, XML storage too. Etc.

Rely to API is not always a good thing. What if the implementation changed? What if the method name changed? API is more fragile than language keywords. So, Microsoft decided to introduce few new keywords that currently rely to the API I mentioned above.

So, the code becomes:

IEnumerable expr = from s in names where s.Length == 5 select s;

You can also write the code in more aestethic way:

IEnumerable expr = from s in names
where s.Length == 5
select s;

The new Keywords are in bold.

As any other Best Practices, if the language already have the keyword to “represent” an API, use the language keyword and not the API directly. Cos API changes more often or new/better API might come. Let the compiler choose which API to be used. Newer compiler will always choose newer/better API. So, you rely to language keyword, not API. If new or better API comes, all you need to do is recompile your code with the newer compiler. No need to REWRITE your code.

Functional Programming way is used cos OOP just simply can’t do it

Here you go! You have LINQ, a very powerful long waited capability in a programming language. You can do queries to “in memory collections”, RDBMS, XML Storage, etc, in one UNIFORM way. The elegance of LINQ can only be achieved via Functional Programming Style. You can’t achieve it by only relying to OOP.

Yes, Type Inferencing, Extension Methods and Lambda Expressions are made to support LINQ. But, since you have it in C#, you can use them for other purposes too. You can do some Functional Programming ways in C#.

I’m not saying OOP is stupid or something. It just it can’t do some stuff where other approach can do. But then OOP still do wonders in area where it can do wonders. 

So, open your mind, folks. Get over your OOP obsession. Learn all programming paradigm!

Tuesday, May 12, 2009

C#代码与JAVASCRIPT函数的相互调用(转载)

问:
1.如何在JavaScript访问C#函数?
2.如何在JavaScript访问C#变量?
3.如何在C#中访问JavaScript的已有变量?
4.如何在C#中访问JavaScript函数?

问题1答案如下:
javaScript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
2、在前台写一个js函数,内容为document.getElementById("btn1").click();
3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数;
方法二:1、函数声明为public
后台代码(把public改成protected也可以)
public string ss()
{
return("a");
}
2、在html里用<%=fucntion()%>可以调用
前台脚本

方法三:1、


方法四:

〈!--用来存储你要调用的函数 --〉
在.CS里有:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";
//根据传回来的值决定调用哪个函数
switch(strFunName)
{
case "enter()":
enter() ; //调用该函数
break;
case "其他":
//调用其他函数
break;
default:
//调用默认函数
break;
}
}
}
public void enter()
{
//……比如计算某值
}
问题2.如何在JavaScript访问C#变量?
答案如下:
方法一:1、通过页面上隐藏域访问
方法二:1、如后台定义了PUBLIC STRING N;前台js中引用该变量的格式为'<%=n%>'或"+<%=n%>+"
方法三:1、或者你可以在服务器端变量赋值后在页面注册一段脚本
""
tmp是后台变量,然后js中可以直接访问temp获得值。

3.如何在C#中访问JavaScript的已有变量?
答案如下:
方法一:1、前台使用静态文本控件隐藏域,将js变量值写入其中;
2、后台用request["id"]来获取值;
方法二:可以用cookie或session

4.如何在C#中访问JavaScript函数?
答案如下:
c#代码中执行javaScript函数:
方法一:1、Page.RegisterStartupScript("ggg","");
方法二:使用Literal类,然后
private void Button2_Click(object sender, System.EventArgs e)
{
string str;
str="";
//Literal1.Visible=true;
Literal1.Text=str;
}