一:一个方法
/// <summary>
///SQL注入过滤
/// </summary>
/// <param name="InText">要过滤的字符串</param>
/// <returns>如果参数存在不安全字符,则返回true</returns>
public static bool SqlFilter2(string InText)
{
string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join|'";
if (InText == null)
return false;
foreach (string str_t in word.Split('|'))
{
if ((InText.ToLower().IndexOf(str_t + " ") > -1) || (InText.ToLower().IndexOf(" " + str_t) > -1) || (InText.ToLower().IndexOf(str_t) > -1))
{
return true;
}
}
return false;
}
二:global.ascx 中的一个方法
/// <summary>
/// 当有数据时交时,触发事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_BeginRequest(Object sender, EventArgs e)
{
//遍历Post参数,隐藏域除外
foreach (string str_t in this.Request.Form)
{
if (str_t == "__VIEWSTATE") continue;
this.goErr(this.Request.Form[str_t].ToString());
}
//遍历Get参数。
foreach (string str_t in this.Request.QueryString)
{
this.goErr(this.Request.QueryString[str_t].ToString());
}
}
三:global.asax中的另外一方法
/// <summary>
/// 校验参数是否存在SQL字符
/// </summary>
/// <param name="tm"></param>
private void goErr(string tm)
{
if (wenlong.function.func.SqlFilter2(tm))
this.Response.Redirect("/error");
//Response.Redirect();
}
我们经常有这样的需求,批量的删除或者选取大量的数据,有非常多的Id值,经常使用in条件查询,如果你使用拼接字符串的方式,可能遭遇SQL语句的长度限制4000个字符。可以使用XML的参数类型来解决。例如下面这个例子。
DECLARE @Ids xml
set @Ids ='<Id>12</Id><Id>1</Id>'
select * from Employees Where EmployeeID in
(
SELECT ParamValues.ID.value('.','int') as Id
FROM @Ids.nodes('/Id') as ParamValues(ID)
)
写个函数来生成Id的xml格式用C#非常容易:
public sealed class PrimaryKeyXMLFormatter
{
public static string FormatXmlForIdArray<T>(Collection<T> idsToList)
{
StringBuilder xmlString = new StringBuilder();
for (int i = 0; i < idsToList.Count; i++)
{
xmlString.AppendFormat("<Id>{0}</Id>", idsToList[i]);
}
return xmlString.ToString();
}
}
从SQL Server 2005新增了XML字段,并且增加了SQL语句直接处理XML字段的功能,也就是说可以直接把 xml 内容存储在该字段中,并且 SQL Server 会把它当作 xml 来对待,而不是当作 varchar 来对待。通过使用SQL语句可以直接获取存放再XML字段中的数据的行集,之后可以使用DataSet或DataTable进行数据处理,当需要写入数据到XML字段时,我们可以使用Modify()函数来实现直接更新数据库。
- SQL Server 中以 Unicode(UTF-16) 来存储 XML 数据。
- XML 字段最多可存储 2G 的数据。
- 可以像插入字符串一样向 XML 字段写入内容。
- 当在 xml 数据类型实例中存储 XML 数据时,不会保留 XML 声明(如 <?xml version='1.0'?>)。
- 插入的 xml 内容的属性的顺序可能会与原 xml 实例的顺序变化。
- 不保留属性值前后的单引号和双引号。
- 不保留命名空间前缀。
- 可以对 XML 字段中的 XML 内容建立索引 http://blog.csdn.net/tjvictor/archive/2009/07/22/4370771.aspx。
- 可以对 XML 字段中的 XML 内容建立约束,比如 age 节点必须大于等于 18。
- 可以通过创建架构来对 XML 进行类型化,比如让 xml 内容的 <user> 节点下面必须有 <fullname> 节点。
xml 数据类型方法
下面谈谈如何查询 xml 数据,注意大小写,另外下面的示例是建立在 T-SQL 基础上的,@xml 变量相当于表中的一个 xml 字段。更多内容请参见 http://msdn.microsoft.com/zh-cn/library/ms190798.aspx。
query
SELECT @xml.query('/Root/ProductDescription/Features')
返回 Features 节点及其子节点。
value
SELECT @xml.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )
返回 Root 节点下面的 ProductDescription 节点中的 ProductID 的属性值。即使只有一个 ProductID,那么也需要显式地指明 [1],表示第一个符合条件的节点。'int' 表示将该属性值转换成 int 类型返回。
exists
SELECT @xml.exist('/Somedate[(text()[1] cast as xs:date ?) = xs:date("2002-01-01Z") ]')
将 /Somedate 文本节点(text())的内容([1])转换成 xs:date 类型(cast as xs:date?),然后与指定的日期进行比较。若相等则返回 1;若不相等则返回 0;若包含 NULL 则返回 NULL。
modify
SET @myDoc.modify('
insert sql:variable("@newFeatures")
into (/Root/ProductDescription/Features)[1] ')
表示将 @newFeatures 插入到 /Root/ProductDescription/Features。
nodes
SELECT T.c.query('.') AS result FROM @xml.nodes('/Root/row') T(c)
将 xml 内容中所有的 /Root/row 节点以关系表格的形式返回,并存储在虚拟表 T 的字段 c 中,然后利用 T.c.query('.') 将 虚拟表 T 的字段 c 中的节点内容查询出来。
Passing lists to SQL Server 2005 with XML Parameters
近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL SERVER.
1.搜索出所有表名,构造为一条SQL语句
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
3.利用微软未公开的存储过程
exec sp_msforeachtable "truncate table ?"
--删除当前数据库所有表中的数据
该方法可以一次清空所有表,但不能加过滤条件.
create PROCEDURE [dbo].[outputdata]
@tablename sysname
AS
declare @column varchar(1000)
declare @columndata varchar(1000)
declare @sql varchar(4000)
declare @xtype tinyint
declare @name sysname
declare @objectId int
declare @objectname sysname
declare @ident int
set nocount on
set @objectId=object_id(@tablename)
if @objectId is null -- 判斷對象是否存在
begin
print 'The object not exists'
return
end
set @objectname=rtrim(object_name(@objectId))
if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密
begin
print 'object not in current database'
return
end
if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判斷對象是否是table
begin
print 'The object is not table'
return
end
select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80
if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' ON'
declare syscolumns_cursor cursor
for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid
open syscolumns_cursor
set @column=''
set @columndata=''
fetch next from syscolumns_cursor into @name,@xtype
while @@fetch_status < >-1
begin
if @@fetch_status < >-2
begin
if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理
begin
set @column=@column+case when len(@column)=0 then'' else ','end+@name
set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','
end
+case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char
when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar
when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime
when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime
when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier
else @name end
end
end
fetch next from syscolumns_cursor into @name,@xtype
end
close syscolumns_cursor
deallocate syscolumns_cursor
set @sql='set nocount on select ''insert '
+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename
print '--'+@sql
exec(@sql)
搜索了几个搜索引擎,找了一些.net的开源系统,(申明一下,下面的看只是个人看法,只供参考)。
一、DotNetNuke DotNetNuke是一个.Net平台下,最负盛名的CMS系统,爱好者们都称它为DNN,开发语言是VB.Net。
Sourceforge上的项目地址:http://sourceforge.net/projects/dnn/
官方网址:http://www.dotnetnuke.com/ ,现在有汉化版本,国内的研究者,主要集中在:http://www.dnnchina.net/,那里提供有很多的学习教程和Skin。 DNN是一个比较成熟的CMS系统,提供有大量的插件(Feed、相册等),目前最高版本是4.0,在.Net2.0框架下运行。
DNN是VB.Net开发的,很多开发者并不习惯VB.Net的风格,所以DNN爱好者创建了个 C#版本的项目SharpNuke.NET。 sourceforge上的地址http://sourceforge.net/projects/sharpnukenet
官方地址为:http://sharpnuke.net/
二、dBlog Sourceforge上说dBlog是asp和asp.net混合开发的,实际上主要还是asp环境下运行的CMS系统,这是一个轻量级的系统,其实 主要用于Blog的发布,而并非Portal。 Sourceforge上的项目地址:http://sourceforge.net/projects/dblog/
官方地址:http://www.dblog.it/ 比较有特点的地方就是blog、podcast的相关功能的实现。
三、Rainbow Portal 一个酷酷的名字--Rainbow,使用C#开发,这个系统是在MS iBuySpy的基础架构上强化而来的,目前的Rainbow2006和iBuySpy项目已经很不一样了,大大的超出了很多,比起DNN来, Rainbow也有不少的优点,它支持多种语言,可以定制主体风格,可以创建工作流等。 Sourceforge上的项目地址:http://sourceforge.net/projects/rainbowportal/
官方地址:http://www.rainbowportal.net/ Rainbow/
在国内也有相关的研究:http://rata.cnblogs.com/
四、OmniPortal OmniPortal并不是一个直接的应用程序,实际上是一个Portal的框架内核,是一个基础框架,可以在它的基础上建立任何的Web应用系统。对于 一个Web开发者,OmniPortal提供了优秀的二次开发基础类库,虽然目前OmniPortal还不是一个Release版本,但是非常值得关注。 Sourceforge上的项目地址:http://sourceforge.net/projects/omniportal/
官方地址:http://www.omniportal.net/
五、Ludico 这个Portal&CMS系统也不是一个正式版本的,去年11月份才开始的项目,但是我发现它的架构非常优秀,采用NHibernate.Net 作为系统框架,因此可以作为一个很好的学习对象,值得关注之。 Sourceforge上的项目地址:http://sourceforge.net/projects/ludico/
强大的插件系统,通过Addin构建成一个功能齐全的.net开发IDE。核心是AddInTree。跟随这个项目开发许多有用的组件,比如功能文本编辑器(ICSharpCode.TextEditor),SharpZipLib等。
链接:http://www.icsharpcode.net/
DotNetNuke
这个就是著名DNN,使用VB.NET进行开发。通过其基本架构可进行堆积木式快速建站。而且支持子网站系统。其由asp.net Portal start kit进化而来。
链接:http://www.dnnchina.net/ ,http://www.dotnetnuke.com
Community Server
这个也是一个很著名的ASP.NET项目,记得好像最早系统原形为asp.net Forums,后来加入了.Text Blog 和nGallery成为一个完整的通用系统。对应的中文版本为宝玉修改的CCS。
链接:http://communityserver.org/
Rainbow
另外一个类似于DNN的系统,使用C#进行开发。
链接:http://www.rainbowportal.net/
RssBandit
一个客户端的RSS查看器,使用C# 进行开发,最大的特点是数据存储采用xml文件。
链接:http://www.rssbandit.org/
FreeTextbox
Web上的一个Html超文本编辑器,早些版本是开源的,现在还是免费使用,不过源代码需要购买。
链接:http://www.freetextbox.com
World Wind
使用.NET开发的一个Windows窗体系统,以地球外观看得角度提供全球定位功能,类似于Google Earth。
链接:http://worldwind.arc.nasa.gov
log4net
对应Java中的log4j。一个强大的日志管理模块。
链接:http://logging.apache.org/log4net/
Monodevelop
非Windows 系统下的.net 平台开发工具。
链接:http://www.monodevelop.com
Paint.NET
使用.net开发的画图软件,功能不错。
链接:http://www.getpaint.net/index.html
Nunit
对应Java中的Junit,非常著名的单元测试工具。
链接:http://www.nunit.org/
FCKeditor
Web上的又一个Html超文本编辑器。
链接:http://www.fckeditor.net/
Nlog
一个日志管理库,类似于Log4Net。
链接:http://www.nlog-project.org/
ManagedSpy
.net 平台下的Spy ++,支持通过.net 2.0开的Windows Forms应用程序。
链接:http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/
Guidance Explore
类似于一个.net平台编程问题简答的FAQ。不过提供的都是英文。
链接:http://www.codeplex.com/Wiki/View.aspx?ProjectName=guidanceExplorer
Terrarium
一个.net开发的多人游戏。玩这个游戏可以提高编程能力^_^。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
TaskVision
任务管理系统,一个经典的Smart Client智能客户端程序。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
IssueVision
事务管理系统,Smart Client智能客户端程序。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
FotoVision
又一个值得学习的.net开发的Windows应用程序。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
Infragistics Tracker Application
由Infragistics开发的Smart Client智能客户端程序。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
Windows Forms RSS Portal
一个.net 开发的聚合器。
链接:http://www.windowsforms.net/Default.aspx?tabindex=4&tabid=49
Enterprise Library
微软的企业库,对原早些时候开发的一些Block 模块进行整合提供企业统一的接口,新版本使用了.net 2.0的的许多功能。
链接:http://msdn.microsoft.com/practices/
PetShop
基于N-tier设计的电子商务网站,没什么好说的了。
链接:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdasamppet4.asp
XmlNotepad
一个用.net 开发的xml文件编辑器。
链接:http://www.microsoft.com/china/msdn/library/data/xml/xmlnotepad.mspx
DockManager Control
一个很不错的实现类似于VS 2005的界面某些功能的组件。
链接:http://www.codeproject.com/vb/net/DockPanelSkin.asp


