这个是一个值得庆贺的时刻——终于,我又可以在上班时间写博客了。
这个项目从跟客户谈需求、文档、编码、测试、发布,全部由我主导。虽然,开发期间至少有一半时间加班至2点,但我还是挺高兴的,收获颇丰。趁着用户测试的一段空闲,整理项目中遇到的一些BUG和一些技巧。
1. JS中进行正则校验:
var pattern =/^ ( -? d + )(.d + ) ? $ / ;
if ( ! pattern.exec(value)) {
alert( ' 格式不正确,请输入数字 ' );
return ;
}
}
2. 存储过程中,动态SQL拼接的问题:
2.1 动态sql与参数拼接
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
程序传进来的@startTime与@endTime是两个字符串,而在拼接动态SQL时,需要在字符串外加两个单引号——SQL中单引号转义为“''”(连续两个单引号)。
2.2 执行动态SQL得到的查询结果保存到局部临时表,记录会被清空
当我们在拼接的动态SQL中,使用如此方法保存结果集“select ...... into #temp from ...... where ...... ”,然后接着在存储过程中检索这个临时表,怎么都得到不到查询结果。为此,我特意将@sqlStr输出一下(print),然后将print出来的语句在查询分析器中执行,却能得到结果。太奇怪了!我将整个存储过程的定义拷到查询分析器中,一段段执行,结果还是这样,#temp根本放不住东西。
而当我不使用动态拼接的SQL,即直接执行select ...... into #temp from ...... where ......,又能得到结果。可问题是,添加where条件时,我有一些逻辑判断,这样直接写死无法实现这个需求。而只能拼接@sqlStr。
后来在网上搜到了一点东西 —— 存储过程中,临时表分局部和全局两种,select ...... into ......得到的是局部临时表,执行:“print @sqlStr exec (@sqlStr) ”得到的结果,会在你没被用到之前释放掉。因此,不论是否能查询到结果,#temp都会是空的。局部的不行,那就只有使用全局的了。在前面定义一张临时表create table #temp (.......),然后执行:“print @sqlStr insert #countStatic exec (@sqlStr) ”,顺利检索到结果。
2.3 求比率时,被除数为零
使用case ... when ... then ... else:
当被除数为0时,改为1;否则为本身(一半当你求所占比率时,如果被除数为0,这个时候除数一般也为0,如此不至于出现统计错误的情况)。
2.4 别被as(可省略)所蒙蔽了
取别名,是写SQL时,常用的做法:
在操作#temp表格时,字段名千万别用TravelTime,会报找不到这个字段的错误。输出这个表格,就可以看到原因了。as作为别名,只会在当前显示结果时,起作用,甚至你在都不能这样操作:
2.5 当你需要在查询时,将两个字段拼接起来,而一个字段是varchar类型,另一个字段是datatime类型,你需要先对datatime字段进行转型操作:e2.customer_name + ' ,' + convert(char,e1.nowdate,102)
3. 保存至数据库前,必须的清理工作(处理特殊字符) :
3.1 当保存的数据中带单引号时
这是一个最容易发生问题的错误,因为我们在拼接SQL时(哦?你说,你从来不做拼接SQL这种低级做法,好吧!跳过这段),对于字符串类型,常常在外边添加单引号,而如果你的内容中包含了单引号,那么问题来了。“'附近有语法错误”,没错,这将是你得到的Exception。写一个方法吧!在拼接之前进行转义的操作:
public static string ToASCII( string value)
{
value = value.Replace( " ' " , " '' " );
value = value.Replace( " " " , " "" " );
return value;
}
4. 一些有点小用途的方法,你可以将它们添加到工具类中,这回让你的代码减少不少冗余:
public static string GetNowDate()
{
return Convert.ToDateTime(DateTime.Now.ToString()).ToString( " yyyy-MM-dd " );
}
// 小数末尾去0
public static string ToInteger( string intStr)
{
string [] str = intStr.Split( ' . ' );
if (str.Length > 1 )
{
while (str[ 1 ].EndsWith( " 0 " ))
{
if (str[ 1 ].Length > 1 )
str[ 1 ] = str[ 1 ].Substring( 0 , str[ 1 ].Length - 1 );
else if (str[ 1 ].Equals( " 0 " ))
return str[ 0 ];
}
return str[ 0 ] + " . " + str[ 1 ];
}
return intStr;
}
// 检测日期格式是否合法
public static bool CheckDate( string date)
{
// null
if (date == null || date.Trim().Length <= 0 )
return false ;
try
{
Convert.ToDateTime(date);
return true ;
}
catch (System.Exception ex)
{
return false ;
}
}