随笔- 7  文章- 4  评论- 1 
2008年5月12日
数据库分页存储过程


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO







/*
名    称: pr_Common_PagedSelect
功    能: 获取指定页的数据

创建标识:2008-05-12 15:30
修改标识:
修改原因:
*/

CREATE    
PROCEDURE [dbo].pr_Common_PagedSelect
    
@TableName      varchar(255),   -- 表名
    @OrderField      varchar(255),   -- 排序字段名
    @SelectedFields  varchar(255),   -- 选择字段名
    @PageSize     int,            -- 页尺寸
    @PageIndex    int,            -- 页码
    @OrderType    bit,            -- 设置排序类型, 非 0 值则降序
    @Condition     varchar(2000)  -- 查询条件 (注意: 不要加 where)
AS
    
declare @strSQL      varchar(6000)       -- 主语句
    declare @strTmp      varchar(1000)       -- 临时变量
    declare @strOrder    varchar(500)        -- 排序类型


    
if @OrderType != 0
        
begin
            
set @strTmp = '<(select min'
            
set @strOrder = ' order by [' + @OrderField + '] desc'
        
end
    
else
        
begin
            
set @strTmp = '>(select max'
            
set @strOrder = ' order by [' + @OrderField +'] asc'
        
end

    
set @strSQL = 'select top ' + str(@PageSize+ ' ' + @SelectedFields + ' from ['
        
+ @TableName + '] where [' + @OrderField + ']' + @strTmp + '(['
        
+ @OrderField + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['
        
+ @OrderField + '] from [' + @TableName + ']' + @strOrder + ') as tblTmp)'
        
+ @strOrder


    
if @Condition != ''
        
Begin
        
set @strSQL = 'select top ' + str(@PageSize+ ' ' + @SelectedFields + ' from ['
            
+ @TableName + '] where [' + @OrderField + ']' + @strTmp + '(['
            
+ @OrderField + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['
            
+ @OrderField + '] from [' + @TableName + '] where ' + @Condition + ' '
            
+ @strOrder + ') as tblTmp) and ' + @Condition + ' ' + @strOrder
        
End
    
if @PageIndex = 1
        
begin
            
set @strTmp = ''
            
if @Condition != ''
                
set @strTmp = ' where (' + @Condition + ')'
    
            
set @strSQL = 'select top ' + str(@PageSize+ ' ' + @SelectedFields + ' from ['
                
+ @TableName + ']' + @strTmp + ' ' + @strOrder
        
end

exec (@strSQL)      --获取记录集







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

posted @ 2008-05-12 16:31 Happiness... 阅读(4) | 评论 (0)编辑
2008年4月25日
     摘要: 1<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">2<htmlxmlns="http://www.w3.org/1999/xhtml">3<head>4<title>div...  阅读全文
posted @ 2008-04-25 15:43 Happiness... 阅读(33) | 评论 (0)编辑
2008年4月24日

在CSDN上有人问:C#中有没有判断一个string类型变量是否为数字类型的系统函数(如vb中的IsNumeric(s))?
答案肯定是没有的。有人提议用int.Parse(string)的方法,然后通过捕获异常来判断返回的值。更好的方法是用正则表达式:


1 public int IsNumeric(string str)
2 {
3       int i;
4       if(str != null && Regex.IsMatch(str,@"^\d+$"))
5                 i = int.Parse(str);
6       else
7                 i = -1;
8        return i;
9 }


 

这应该是个好办法。不过如果要提出更高的要求,例如不仅要判断字符串是否全为数字,还要在判断后,要求将其进行转换。那么上述函数就有缺陷,因为在上面的正则表达式中无法判断‘-’符号。因此上面的函数只能转换非负数。尤有甚者,要求转换的不仅是整数,还包括浮点数,尤其是科学计数法的字符串,例如:string s = "-3.14159E+10"

这里面的'E'、'.'、'+'等字符,都是比较特殊的,而且还要求它们出现的位置要符合浮点数或科学计数方法的规定。那么就不能使用正则表达式了,至少很麻烦(我是这样认为的,或许有好的方法)。问题还是回到最初的解决方案,就是使用Parse()方法和捕获异常。以下是实现此功能的方法:

 

 

 1 public bool IsNumeric(string s, out double result)
 2 {
 3    bool bReturn = true;
 4    try
 5    {
 6        result = double.Parse(s);
 7    }
 8    catch
 9    {
10        result = 0;
11        bReturn = false;
12    }
13    return bReturn;      
14 }
15 例如调用:
16 
17 string s1="abc";
18 string s2="-3.14159E+10";
19 double d1,d2;
20 bool ty1 = IsNumeric(s1,out d1);
21 bool ty2 = IsNumeric(s2,out d2);
22 结果:
23 ty1=false;d1=0;
24 ty2=true;d2=31415900000
25 



说明:因为涉及到浮点数,就不能使用-1来做为出现错误的返回值,如前面的例子。也就是说本方法有两个返回值,一个是判断转换是否成功,为bool型;另一个是转换成功后的浮点数值。我用out来解决该问题。

本方法仍然适合整型,只需对返回值进行强制转换即可。

posted @ 2008-04-24 17:23 Happiness... 阅读(11) | 评论 (1)编辑
     摘要: 1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Text;45namespaceChinaValue.CommonV20086{7/**////<summary>8///将字符串转换成任意其他数据9///</summary>10publicstaticclassConvertStr11{12/**////...  阅读全文
posted @ 2008-04-24 16:58 Happiness... 阅读(13) | 评论 (0)编辑
     摘要: 1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Text;4usingSystem.Text.RegularExpressions;56namespaceChinaValue.CommonV20087{8/**////<summary>9///各种数据检测的逻辑10///</summary>11publi...  阅读全文
posted @ 2008-04-24 16:55 Happiness... 阅读(20) | 评论 (0)编辑
     摘要: Request1usingSystem;2usingSystem.Web;3usingSystem.Text;4usingSystem.Text.RegularExpressions;56namespaceChinaValue.CommonV20087{8/**////<summary>9///Request操作类10///</summary>11publicclassCV...  阅读全文
posted @ 2008-04-24 16:54 Happiness... 阅读(18) | 评论 (0)编辑
     摘要: UTILITYUtility1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Text;4usingMicrosoft.Practices.EnterpriseLibrary.Logging;5usingSystem.Security.Cryptography;6usingSystem.Web;7usingSystem.Colle...  阅读全文
posted @ 2008-04-24 16:28 Happiness... 阅读(19) | 评论 (0)编辑