濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享

Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享

熱門標(biāo)簽:塔城代理外呼系統(tǒng) 代理接電話機(jī)器人如何取消 濮陽(yáng)外呼電銷系統(tǒng)怎么樣 天心智能電銷機(jī)器人 400電話辦理哪家性價(jià)比高 地圖定位圖標(biāo)標(biāo)注 地圖標(biāo)注的公司有哪些 地圖標(biāo)注專業(yè)團(tuán)隊(duì) 遂寧市地圖標(biāo)注app

廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所述:

 /// summary>
 /// 類型轉(zhuǎn)換類
 /// 處理數(shù)據(jù)庫(kù)獲取字段為空的情況
 /// /summary>
 public static class DBConvert
 {
  #region------------------ToInt32類型轉(zhuǎn)換------------------
  /// summary>
  /// 讀取數(shù)據(jù)庫(kù)中字符串并轉(zhuǎn)換成Int32
  /// 為空時(shí)返回0
  /// /summary>
  /// param name="obj">object類型的值/param>
  /// returns>Int32類型/returns>
  public static int ToInt32(object obj)
  {
   int result = 0;
   if (IsInt(Convert.ToString(obj)))
   {
    result = Convert.ToInt32(obj);
   }
   else if (obj != null  obj is Enum) //處理非null值類型時(shí)(或者枚舉)
   {
    result = ((IConvertible)obj).ToInt32(null);
   }
   return result;
  }
  /// summary>
  /// 讀取數(shù)據(jù)庫(kù)中字符串并轉(zhuǎn)換成Int32
  /// 為空時(shí)返回0
  /// /summary>
  /// param name="str">string類型的值/param>
  /// returns>Int32類型/returns>
  public static int ToInt32(string str)
  {
   int result = 0;
   if (IsInt(str))
   {
    result = Convert.ToInt32(str);
   }
   return result;
  }
  /// summary>
  /// 判斷一個(gè)字符串是否屬于Int類型
  /// 如果是的返回true,如果不是返回false
  /// /summary>
  /// param name="str">string類型的值/param>
  /// returns>true:是Int的字符串(即可以轉(zhuǎn)換成Int類型),false:不是Int類型的字符串/returns>
  public static bool IsInt(string str)
  {
   bool result = false;
   if (str != ""  str!=null)
   {
    Regex reg = new Regex("^[0-9]*$");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  #endregion
  #region------------------ToString類型轉(zhuǎn)換------------------
  /// summary>
  /// 讀取數(shù)據(jù)庫(kù)中字符串并轉(zhuǎn)換成string
  /// /summary>
  /// param name="obj">object類型的值/param>
  /// returns>string類型/returns>
  public static string ToString(object obj)
  {
   string result = "";
   if (obj != null)
   {
    result = Convert.ToString(obj);
   }
   return result;
  }
  #endregion
  #region------------------ToDouble類型轉(zhuǎn)換------------------
  /// summary>
  /// 判斷一個(gè)字符串是否屬于Double類型(包括負(fù)浮點(diǎn)型)
  /// 如果是的返回true,如果不是返回false
  /// /summary>
  /// param name="str">string類型的值/param>
  /// returns>true:是Double的字符串(即可以轉(zhuǎn)換成Double類型),false:不是Double類型的字符串/returns>
  public static bool IsDouble(string str)
  {
   bool result = false;
   if (str != ""  str != null)
   {
    Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  /// summary>
  /// 讀取數(shù)據(jù)庫(kù)中字符串并轉(zhuǎn)換成Int32
  /// 為空時(shí)返回0
  /// /summary>
  /// param name="obj">object類型的值/param>
  /// returns>Int32類型/returns>
  public static double ToDouble(object obj)
  {
   double result = 0.0;
   if (IsDouble(Convert.ToString(obj)))
   {
    result = Convert.ToDouble(obj);
   }
   else if (obj != null  obj is Enum) //處理枚舉
   {
    result = ((IConvertible)obj).ToDouble(null);
   }
   return result;
  }
  /// summary>
  /// 讀取數(shù)據(jù)庫(kù)中字符串并轉(zhuǎn)換成Int32
  /// 為空時(shí)返回0
  /// /summary>
  /// param name="str">string類型的值/param>
  /// returns>Int32類型/returns>
  public static double ToDouble(string str)
  {
   double result = 0.0;
   if (IsDouble(str))
   {
    result = Convert.ToDouble(str);
   }
   return result;
  }
  #endregion
  #region------------------ToDateTime類型轉(zhuǎn)換------------------
  /// summary>
  /// 判斷時(shí)間格式是否是時(shí)間類型
  /// 如23:10
  /// /summary>
  /// param name="str">要判斷的字符串/param>
  /// returns>true:是時(shí)間類型的字符串(即可以轉(zhuǎn)換成時(shí)間類型),false:不是時(shí)間類型的字符串/returns>
  public static bool isDateTime(string str)
  {
   bool result = false;
   if (str != ""  str != null)
   {
    Regex reg = new Regex("(([01]\\d)|(2[0-3])):[0-5]\\d");
    if (reg.IsMatch(str))
    {
     result = true;
    }
   }
   return result;
  }
  #endregion
 }
}
//"^\d+(\.\d+)?$"  //非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
//"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點(diǎn)數(shù)
//"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
//"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負(fù)浮點(diǎn)數(shù)
//"^(-?\d+)(\.\d+)?$"  //浮點(diǎn)數(shù)

好了,Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享到此結(jié)束。

下面看下ASP.NET頁(yè)面數(shù)據(jù)驗(yàn)證通用類的實(shí)例代碼。

public class PageValidate
{
private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等價(jià)于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或數(shù)字的字符串,和 [a-zA-Z0-9] 語(yǔ)法一樣
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
public PageValidate()
{
}
//數(shù)字字符串檢查#region 數(shù)字字符串檢查
public static bool IsPhone(string inputData)
{
Match m = RegPhone.Match(inputData);
return m.Success;
}
/**//// summary>
/// 檢查Request查詢字符串的鍵值,是否是數(shù)字,最大長(zhǎng)度限制
/// /summary>
/// param name="req">Request/param>
/// param name="inputKey">Request的鍵值/param>
/// param name="maxLen">最大長(zhǎng)度/param>
/// returns>返回Request查詢字符串/returns>
public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
{
string retVal = string.Empty;
if(inputKey != null  inputKey != string.Empty)
{
retVal = req.QueryString[inputKey];
if(null == retVal)
retVal = req.Form[inputKey];
if(null != retVal)
{
retVal = SqlText(retVal, maxLen);
if(!IsNumber(retVal))
retVal = string.Empty;
}
}
if(retVal == null)
retVal = string.Empty;
return retVal;
}
/**//// summary>
/// 是否數(shù)字字符串
/// /summary>
/// param name="inputData">輸入字符串/param>
/// returns>/returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
/**//// summary>
/// 是否數(shù)字字符串 可帶正負(fù)號(hào)
/// /summary>
/// param name="inputData">輸入字符串/param>
/// returns>/returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
/**//// summary>
/// 是否是浮點(diǎn)數(shù)
/// /summary>
/// param name="inputData">輸入字符串/param>
/// returns>/returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
/**//// summary>
/// 是否是浮點(diǎn)數(shù) 可帶正負(fù)號(hào)
/// /summary>
/// param name="inputData">輸入字符串/param>
/// returns>/returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
#endregion
//中文檢測(cè)#region 中文檢測(cè)
/**//// summary>
/// 檢測(cè)是否有中文字符
/// /summary>
/// param name="inputData">/param>
/// returns>/returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
//郵件地址#region 郵件地址
/**//// summary>
/// 是否是浮點(diǎn)數(shù) 可帶正負(fù)號(hào)
/// /summary>
/// param name="inputData">輸入字符串/param>
/// returns>/returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
#endregion
//其他#region 其他
/**//// summary>
/// 檢查字符串最大長(zhǎng)度,返回指定長(zhǎng)度的串
/// /summary>
/// param name="sqlInput">輸入字符串/param>
/// param name="maxLength">最大長(zhǎng)度/param>
/// returns>/returns>
public static string SqlText(string sqlInput, int maxLength)
{
if(sqlInput != null  sqlInput != string.Empty)
{
sqlInput = sqlInput.Trim();
if(sqlInput.Length > maxLength)//按最大長(zhǎng)度截取字符串
sqlInput = sqlInput.Substring(0, maxLength);
}
return sqlInput;
}
/**//// summary>
/// 字符串編碼
/// /summary>
/// param name="inputData">/param>
/// returns>/returns>
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
/**//// summary>
/// 設(shè)置Label顯示Encode的字符串
/// /summary>
/// param name="lbl">/param>
/// param name="txtInput">/param>
public static void SetLabel(Label lbl, string txtInput)
{
lbl.Text = HtmlEncode(txtInput);
}
public static void SetLabel(Label lbl, object inputObj)
{
SetLabel(lbl, inputObj.ToString());
}
//字符串清理
public static string InputText(string inputString, int maxLength)
{
StringBuilder retVal = new StringBuilder();
// 檢查是否為空
if ((inputString != null)  (inputString != String.Empty))
{
inputString = inputString.Trim();
//檢查長(zhǎng)度
if (inputString.Length > maxLength)
inputString = inputString.Substring(0, maxLength);
//替換危險(xiǎn)字符
for (int i = 0; i  inputString.Length; i++)
{
switch (inputString[i])
{
case '"':
retVal.Append("quot;");
break;
case '':
retVal.Append("lt;");
break;
case '>':
retVal.Append("gt;");
break;
default:
retVal.Append(inputString[i]);
break;
}
}
retVal.Replace("'", " ");// 替換單引號(hào)
}
return retVal.ToString();
}
/**//// summary>
/// 轉(zhuǎn)換成 HTML code
/// /summary>
/// param name="str">string/param>
/// returns>string/returns>
public static string Encode(string str)
{
str = str.Replace("","");
str = str.Replace("'","''");
str = str.Replace("\"","quot;");
str = str.Replace(" ","nbsp;");
str = str.Replace("","lt;");
str = str.Replace(">","gt;");
str = str.Replace("\n","br>");
return str;
}
/**//// summary>
///解析html成 普通文本
/// /summary>
/// param name="str">string/param>
/// returns>string/returns>
public static string Decode(string str)
{
str = str.Replace("br>","\n");
str = str.Replace("gt;",">");
str = str.Replace("lt;","");
str = str.Replace("nbsp;"," ");
str = str.Replace("quot;","\"");
return str;
}
public static string SqlTextClear(string sqlText)
{
if (sqlText == null)
{
return null;
}
if (sqlText == "")
{
return "";
}
sqlText = sqlText.Replace(",", "");//去除,
sqlText = sqlText.Replace("", "");//去除
sqlText = sqlText.Replace(">", "");//去除>
sqlText = sqlText.Replace("--", "");//去除--
sqlText = sqlText.Replace("'", "");//去除'
sqlText = sqlText.Replace("\"", "");//去除"
sqlText = sqlText.Replace("=", "");//去除=
sqlText = sqlText.Replace("%", "");//去除%
sqlText = sqlText.Replace(" ", "");//去除空格
return sqlText;
}
#endregion
//是否由特定字符組成#region 是否由特定字符組成
public static bool isContainSameChar(string strInput)
{
string charInput = string.Empty;
if (!string.IsNullOrEmpty(strInput))
{
charInput = strInput.Substring(0, 1);
}
return isContainSameChar(strInput, charInput, strInput.Length);
}
public static bool isContainSameChar(string strInput, string charInput, int lenInput)
{
if (string.IsNullOrEmpty(charInput))
{
return false;
}
else
{
Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
//Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
Match m = RegNumber.Match(strInput);
return m.Success;
}
}
#endregion
//檢查輸入的參數(shù)是不是某些定義好的特殊字符:這個(gè)方法目前用于密碼輸入的安全檢查#region 檢查輸入的參數(shù)是不是某些定義好的特殊字符:這個(gè)方法目前用于密碼輸入的安全檢查
/**//// summary>
/// 檢查輸入的參數(shù)是不是某些定義好的特殊字符:這個(gè)方法目前用于密碼輸入的安全檢查
/// /summary>
public static bool isContainSpecChar(string strInput)
{
string[] list = new string[] { "123456", "654321" };
bool result = new bool();
for (int i = 0; i  list.Length; i++)
{
if (strInput == list[i])
{
result = true;
break;
}
}
return result;
}
#endregion
}
您可能感興趣的文章:
  • JS實(shí)現(xiàn)將Asp.Net的DateTime Json類型轉(zhuǎn)換為標(biāo)準(zhǔn)時(shí)間的方法
  • asp與js的類型轉(zhuǎn)換函數(shù)介紹
  • asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼
  • asp 類型轉(zhuǎn)換函數(shù)大全
  • ASP.NET2.0服務(wù)器控件之類型轉(zhuǎn)換器

標(biāo)簽:本溪 麗江 重慶 婁底 吉林 河南 汕頭 宜春

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享》,本文關(guān)鍵詞  Asp.Net,類型,轉(zhuǎn)換,類,通用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Asp.Net類型轉(zhuǎn)換類(通用類)代碼分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    平和县| 民县| 宁阳县| 龙州县| 布拖县| 高邑县| 寿光市| 昌江| 景洪市| 无极县| 五莲县| 沧州市| 上饶县| 六枝特区| 本溪| 中牟县| 呼伦贝尔市| 奉贤区| 乳山市| 孝感市| 丘北县| 齐齐哈尔市| 海原县| 河北省| 诸暨市| 林西县| 吴旗县| 阳江市| 潜山县| 新沂市| 财经| 彩票| 宁阳县| 通化县| 芦山县| 新密市| 贵阳市| 平武县| 民乐县| 汪清县| 普兰店市|