2008年6月小记
1、SP拆分字符串,如把a1,a2,a3以,分隔拆分出来
2、在IIS7中如何继续使用URLRewriter及ApplicationErrorLog
在<system.web>中配置也要在<system.webServer>中添加一份
如有如下的配置
3、去除IIS7的30MB上传限制
修改文件C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml
<attribute name="maxAllowedContentLength" type="uint" defaultValue="30000000" />
加个0就是300MB
4、通过子类构建父类
5、Linq to SQL中如何解决"Specified cast is not valid"的问题
在使用Linq to SQL中对象属性与数据库中表字段的类型转换无效的问题 ,例如:数据表字段Audit的类型为tinyint,它对应的对象是
6、 异步调用asmx的Web方法
引用asmx页面,VS会自动生成代理,除了原生的方法外例如PingPost,还会自动生PingPostAsync这样以Async尾的相对应异常调用方法,不过使用这个异步方法的页面还是需要设置一下
7、SQL常用语句收集
8、判断一个js对象是否未定义:if(typeof(kk) == 'undefined')
9、使用UriBuilder移除url参数
12、使用StringComparer创建大小写无关的字典:
13、排重表的重复行
ALTER PROCEDURE [dbo].[ec_System_SplitString]
@strs nvarchar(4000),
@separator nchar(1)=','
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tbNames table([Name] nvarchar(256) NOT NULL PRIMARY KEY)
DECLARE @Num int;
DECLARE @Pos int;
DECLARE @NextPos int;
DECLARE @Name nvarchar(256);
SET @Num = 0;
SET @Pos = 1;
WHILE(@Pos <= LEN(@strs))
BEGIN
SELECT @NextPos = CHARINDEX(@separator, @strs, @Pos)
IF (@NextPos = 0 OR @NextPos IS NULL)
SELECT @NextPos = LEN(@strs) + 1
SELECT @Name = RTRIM(LTRIM(SUBSTRING(@strs, @Pos, @NextPos - @Pos)))
SELECT @Pos = @NextPos+1
INSERT INTO @tbNames VALUES (@Name)
SET @Num = @Num + 1
END
SELECT [Name] FROM @tbNames
END
@strs nvarchar(4000),
@separator nchar(1)=','
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tbNames table([Name] nvarchar(256) NOT NULL PRIMARY KEY)
DECLARE @Num int;
DECLARE @Pos int;
DECLARE @NextPos int;
DECLARE @Name nvarchar(256);
SET @Num = 0;
SET @Pos = 1;
WHILE(@Pos <= LEN(@strs))
BEGIN
SELECT @NextPos = CHARINDEX(@separator, @strs, @Pos)
IF (@NextPos = 0 OR @NextPos IS NULL)
SELECT @NextPos = LEN(@strs) + 1
SELECT @Name = RTRIM(LTRIM(SUBSTRING(@strs, @Pos, @NextPos - @Pos)))
SELECT @Pos = @NextPos+1
INSERT INTO @tbNames VALUES (@Name)
SET @Num = @Num + 1
END
SELECT [Name] FROM @tbNames
END
2、在IIS7中如何继续使用URLRewriter及ApplicationErrorLog
在<system.web>中配置也要在<system.webServer>中添加一份
如有如下的配置
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>
</system.web>
同时也应该在<system.webServer>中添加一份<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</modules>
</system.webServer>
ApplicationErrorLog配置<modules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</modules>
</system.webServer>
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="ErrorLog.aspx" type="Lion.Web.ApplicationErrorLog.ErrorLogPageFactory, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Lion.Web.ApplicationErrorLog.ErrorLogModule, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</httpModules>
</system.web>
<system.webServer>
<handlers>
<add name="ApplicationErrorLog" verb="POST,GET,HEAD" path="ErrorLog.aspx" type="Lion.Web.ApplicationErrorLog.ErrorLogPageFactory, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</handlers>
<modules>
<add name="ErrorLog" type="Lion.Web.ApplicationErrorLog.ErrorLogModule, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</modules>
</system.webServer>
<httpHandlers>
<add verb="POST,GET,HEAD" path="ErrorLog.aspx" type="Lion.Web.ApplicationErrorLog.ErrorLogPageFactory, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</httpHandlers>
<httpModules>
<add name="ErrorLog" type="Lion.Web.ApplicationErrorLog.ErrorLogModule, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</httpModules>
</system.web>
<system.webServer>
<handlers>
<add name="ApplicationErrorLog" verb="POST,GET,HEAD" path="ErrorLog.aspx" type="Lion.Web.ApplicationErrorLog.ErrorLogPageFactory, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</handlers>
<modules>
<add name="ErrorLog" type="Lion.Web.ApplicationErrorLog.ErrorLogModule, Lion.Web.ApplicationErrorLog, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</modules>
</system.webServer>
3、去除IIS7的30MB上传限制
修改文件C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml
<attribute name="maxAllowedContentLength" type="uint" defaultValue="30000000" />
加个0就是300MB
4、通过子类构建父类
public class RecordHistory : RecordMetadata
private RecordHistory CopyToRecordHistory(RecordMetadata metadata)
{
//使用反射把RecordMetadata导入到RecordHistory
RecordHistory history = null;
if (metadata != null)
{
history = new RecordHistory();
Type historyType = history.GetType();
Type metadataType = metadata.GetType();
PropertyInfo[] historyProperties = historyType.GetProperties();
foreach (PropertyInfo historyProperty in historyProperties)
{
PropertyInfo metadataProperty = metadataType.GetProperty(historyProperty.Name);
if (metadataProperty != null)
{
metadataProperty.SetValue(history, metadataProperty.GetValue(metadata, null), null);
}
}
}
return history;
}
{
//使用反射把RecordMetadata导入到RecordHistory
RecordHistory history = null;
if (metadata != null)
{
history = new RecordHistory();
Type historyType = history.GetType();
Type metadataType = metadata.GetType();
PropertyInfo[] historyProperties = historyType.GetProperties();
foreach (PropertyInfo historyProperty in historyProperties)
{
PropertyInfo metadataProperty = metadataType.GetProperty(historyProperty.Name);
if (metadataProperty != null)
{
metadataProperty.SetValue(history, metadataProperty.GetValue(metadata, null), null);
}
}
}
return history;
}
5、Linq to SQL中如何解决"Specified cast is not valid"的问题
在使用Linq to SQL中对象属性与数据库中表字段的类型转换无效的问题 ,例如:数据表字段Audit的类型为tinyint,它对应的对象是
[Column(Name = "Audit")]
public Int16 Audit { get; set; }
但是这样在query时会出现"Specified cast is not valid"的错误,其实这是在定义DataContext对象时没有指定DbType所致,所以定义可以改为public Int16 Audit { get; set; }
[Column(Name = "Audit", DbType = "tinyint")]
public Int16 Audit { get; set; }
public Int16 Audit { get; set; }
6、 异步调用asmx的Web方法
引用asmx页面,VS会自动生成代理,除了原生的方法外例如PingPost,还会自动生PingPostAsync这样以Async尾的相对应异常调用方法,不过使用这个异步方法的页面还是需要设置一下
<%@ Page Async="true" %>
7、SQL常用语句收集
8、判断一个js对象是否未定义:if(typeof(kk) == 'undefined')
9、使用UriBuilder移除url参数
Response.Output.Write(new UriBuilder(Request.Url)
{
Query=null
}.ToString());
{
Query=null
}.ToString());
http://localhost:46630/CSDN.ExpertCenter.WebTest/Demos/B.aspx?username=billok&count=12 ==>
http://localhost:46630/CSDN.ExpertCenter.WebTest/Demos/B.aspx
通过扩展UriBuilder类来增强功能的类还有UrlBuilder
另外:VirtualPathUtility 类为涉及虚拟路径的常见操作提供实用工具方法。
VirtualPathUtility.IsAppRelative 判断是否为虚拟路径.
HttpRuntime 和 HostingEnvironment 都是很实用的辅助工具。
Path.IsPathRooted 判断是不是为绝对路径
if (!Path.IsPathRooted(path))
path = HttpContext.Current.Server.MapPath(path);
path = HttpContext.Current.Server.MapPath(path);
10、将访问Windows文件夹变为磁盘盘符
11、通过FileIOPermission进行代码访问权限检测
FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read,@"c:\billchen.txt");
permission.Demand();
permission.Demand();
12、使用StringComparer创建大小写无关的字典:
Dictionary<string, int> dic = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
dic["Test"] = 10;
int n = dic["test"];
dic["Test"] = 10;
int n = dic["test"];
13、排重表的重复行
select a.username,a.tagname,a.tagtype into temp
from usertags a
group by a.username,a.tagname,a.tagtype
truncate table UserTags
insert into UserTags(username,tagname,tagtype)
select username,tagname,tagtype
from temp
drop table temp
from usertags a
group by a.username,a.tagname,a.tagtype
truncate table UserTags
insert into UserTags(username,tagname,tagtype)
select username,tagname,tagtype
from temp
drop table temp