1,連接字符串的操作
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connString" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=ZJMo"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="DefaultDataSource" value="user id=sa; password=sa; database=ZJMo; server=.;"/>
</appSettings>
</configuration>
上面是App.config中的xml代碼,下面是cs文中中進行讀取方法
//protected static string connectionString = ConfigurationSettings.AppSettings["DefaultDataSource"];//.net1.1
protected static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString();
注:第一行是在.net1.1是用的方式讀取appsettings里的值,而下面是在.net2.0時讀取connectionstrings里的值.
2,一個用正剛表達式來驗證Strings的CLASS
class ClassCheck
{
public ClassCheck()
{
//構造function
}
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]+$"); //等價於^[+-]?\d+[.]?\d+$ 帶正負號的小數
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或數字的字符串,和 [a-zA-Z0-9] 語法一樣
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); //中文字
#region 是否數字字符串
/// <summary>
/// 是否數字字符串
/// </summary>
/// <param name="inputData">輸入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
#endregion
#region 是否數字字符串 可帶正負號
/// <summary>
/// 是否數字字符串 可帶正負號
/// </summary>
/// <param name="inputData">輸入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
#endregion
#region 是否是浮點數
/// <summary>
/// 是否是浮點數
/// </summary>
/// <param name="inputData">輸入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
#endregion
#region 是否是浮點數 可帶正負號
/// <summary>
/// 是否是浮點數 可帶正負號
/// </summary>
/// <param name="inputData">輸入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
#endregion
#region 檢測是否有中文字符
/// <summary>
/// 檢測是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
#region 檢查郵件地址
/// <summary>
/// 是否是浮點數 可帶正負號
/// </summary>
/// <param name="inputData">輸入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
#endregion
#region 按要求長度截取字符串
/// <summary>
/// 檢查字符串最大長度,返回指定長度的串
/// </summary>
/// <param name="sqlInput">輸入字符串</param>
/// <param name="maxLength">最大長度</param>
/// <returns></returns>
public static string SupStringText(string stringInput, int maxLength)
{
if (stringInput != null && stringInput != string.Empty)
{
stringInput = stringInput.Trim();
if (stringInput.Length > maxLength)//按最大長度截取字符串
stringInput = stringInput.Substring(0, maxLength);
}
return stringInput;
}
#endregion