c# 使用this关键字扩展方法,验证数据,复制可用
调用示例1:
if (str.isNullOrEmpty()) throw new Exception("请输入内容");
if (str.isNumber()==false) throw new Exception("输入的内容不是数字类型");
int aa = str.isToBool_Int();
decimal bb = str.isToNumber();
string cc = str.isNullOrEmptyToStr();
string dd = str.isNullOrEmptyToStr("999");
string ee = str.ToRightString(3);
object ff = str.ToSQL_TrimStringDBNull();
object gg = str.isToNumberDBNull();
string hh = str.ToString_Add0(3);
byte[] kk = str.ToByteArray();
调用示例2:
var para2 = new List<SqlParameter>();
para2.Add(new SqlParameter("type", type.isToNumber_Int(-1)));
para2.Add(new SqlParameter("crid", crid.ToSQL_TrimString()));
para2.Add(new SqlParameter("tcode", tcode.isNullOrEmptyToStr("999")));
para2.Add(new SqlParameter("whid", whid.ToRightString(3)));
para2.Add(new SqlParameter("suNum", suNum.isToNumberDBNull()));
para2.Add(new SqlParameter("ckDate", ckDate.is_ToDatetime(false)));
para2.Add(new SqlParameter("isuse", isuse.isToNumber_Int()));
完整代码,直接copy可用:
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Controller
{
public static class Normal
{
public static String ToSQLString(this Object source)
{
return Convert.ToString(source).Replace("'", "''").Trim();
}
public static String ToSQL_TrimString(this Object source)
{
return Convert.ToString(source).Replace("'", "''").Trim();
}
public static object ToSQL_TrimStringDBNull(this Object source,bool is_Error=false)
{
try
{
if (source == null) return DBNull.Value;
if (source.ToSQLString().Trim() == "") return DBNull.Value;
return Convert.ToString(source).Replace("'", "''").Trim();
}
catch (Exception)
{
if (is_Error)
throw new Exception("日期格式错误");
else return DBNull.Value;
}
}
public static void ToSqlStringPara(params string[] arg)
{
}
public static bool isNumber(this Object source)
{
decimal d = 0;
if (!decimal.TryParse(Convert.ToString(source), out d))
{
return false;
}
return true;
}
public static decimal isToNumber(this Object source,decimal outStr=0)
{
decimal d = 0;
if (!decimal.TryParse(Convert.ToString(source), out d))
{
return outStr;
}
return decimal.Parse(Convert.ToString(source));
}
public static object isToNumberDBNull(this Object source)
{
decimal d = 0;
if (!decimal.TryParse(Convert.ToString(source), out d))
{
return DBNull.Value;
}
return decimal.Parse(Convert.ToString(source));
}
public static int isToNumber_Int(this Object source, int outStr = 0)
{
int d = 0;
if (!int.TryParse(Convert.ToString(source), out d))
{
return outStr;
}
return int.Parse(Convert.ToString(source));
}
public static bool isNullOrEmpty(this Object source)
{
if (source == null) return true;
if (source.ToString() == "") return true;
return false;
}
public static List<object> toToListObj(this DataTable source)
{
List<object> lobj = new List<object>();
if (source == null) return lobj;
if (source.ToString() == "") return lobj;
for (int i = 0; i < source.Rows.Count; i++)
{
lobj.Add(source.Rows[i][0]);
}
return lobj;
}
public static string isNullOrEmptyToStr(this Object source,string defaultStr="")
{
if (source == null) return defaultStr;
if (source.ToString() == "") return defaultStr;
return source.ToString();
}
public static string isNullOrEmptyToSqlStr(this Object source, string defaultStr = "")
{
if (source == null) return defaultStr.Replace("'", "''").Trim(); ;
if (source.ToString() == "") return defaultStr.Replace("'", "''").Trim(); ;
return source.ToString().Replace("'", "''").Trim(); ;
}
public static object is_ToDatetime(this string source,bool FormatHourFM=true,bool is_Error=false)
{
try
{
if (source == null) return DBNull.Value;
if (source.ToSQLString() == "") return DBNull.Value;
if (source.ToSQLString().IndexOf("1900") >= 0) return DBNull.Value;
if (FormatHourFM)
return Convert.ToDateTime(source).ToString("yyyy-MM-dd HH:mm:ss");
else
return Convert.ToDateTime(source).ToString("yyyy-MM-dd");
}
catch (Exception)
{
if(is_Error)
throw new Exception("日期格式错误") ;
else return DBNull.Value;
}
}
public static object is_ToDate_NullStr(this string source, bool FormatHourFM = true, bool is_Error = false)
{
try
{
if (source == null) return "null";
if (source.ToSQLString() == "") return "null";
if (source.ToSQLString().IndexOf("1900") >= 0) return "null";
if (FormatHourFM)
return "'"+ Convert.ToDateTime(source).ToString("yyyy-MM-dd HH:mm:ss") + "'";
else
return "'" + Convert.ToDateTime(source).ToString("yyyy-MM-dd") + "'";
}
catch (Exception)
{
if (is_Error)
throw new Exception("日期格式错误");
else return DBNull.Value;
}
}
public static int isToBool_Int(this Object source)
{
return source.ToString() == "是" ? 1 : 0;
}
public static string ToRightString(this string source, int len)
{
if (source == null) return "";
return source.Substring(source.Length - len);
}
public static string ToString_Add0(this Object source, int len)
{
string rtnStr = "";
if (source == null) return "";
rtnStr = ("000000" + source);
return rtnStr.Substring(rtnStr.Length - len);
}
public static byte[] ToByteArray(this Object source)
{
if (source == null) return null;
if (typeof(System.String).Equals(source.GetType()))
{
return Convert.FromBase64String(source.ToString());
}
else
{
if (Convert.IsDBNull(source))
{
return null;
}
else
{
return (byte[])source;
}
}
}
}
}