C# 数据验证
#region 验证
/// <summary>
/// 验证所修改或添加的信息是否已经存在,针对数据库表中唯一字段
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="keyName">字段名</param>
/// <param name="key">值</param>
/// <returns>true:已经存在;false:不存在</returns>
private bool isExist(string tableName, string keyName, string key)
{
string sql = "select * from " + tableName + " where " + keyName + "='" + key + "'";
DataTable dt = new DataTable();
try
{
//执行SQL得到DataTable
//......
if (dt.Rows.Count > 0)
return true;
else
return false;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
db.Close();
}
}
/// <summary>
/// 匹配由字母、数字、下划线、汉字组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexCommonString(string str)
{
Regex exStr = new Regex(@"^[\da-zA-Z_\u4e00-\u9fa5]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配数字字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexNumberString(string str)
{
Regex exStr = new Regex(@"^-?[0-9]*\.?[0-9]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配数字、字母组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexNumberLetterString(string str)
{
Regex exStr = new Regex(@"^[\da-zA-Z]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配字母组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexLetterString(string str)
{
Regex exStr = new Regex(@"^[a-zA-Z]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 验证所修改或添加的信息是否已经存在,针对数据库表中唯一字段
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="keyName">字段名</param>
/// <param name="key">值</param>
/// <returns>true:已经存在;false:不存在</returns>
private bool isExist(string tableName, string keyName, string key)
{
string sql = "select * from " + tableName + " where " + keyName + "='" + key + "'";
DataTable dt = new DataTable();
try
{
//执行SQL得到DataTable
//......
if (dt.Rows.Count > 0)
return true;
else
return false;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
db.Close();
}
}
/// <summary>
/// 匹配由字母、数字、下划线、汉字组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexCommonString(string str)
{
Regex exStr = new Regex(@"^[\da-zA-Z_\u4e00-\u9fa5]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配数字字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexNumberString(string str)
{
Regex exStr = new Regex(@"^-?[0-9]*\.?[0-9]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配数字、字母组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexNumberLetterString(string str)
{
Regex exStr = new Regex(@"^[\da-zA-Z]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配字母组成的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
private bool regexLetterString(string str)
{
Regex exStr = new Regex(@"^[a-zA-Z]+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// <summary>
/// 匹配正整数的字符串
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
public static bool regexPlusNumberString(string str)
{
Regex exStr = new Regex(@"^\d+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
/// </summary>
/// <param name="str">进行匹配的字符串</param>
/// <returns>true:匹配成功;false:匹配失败</returns>
public static bool regexPlusNumberString(string str)
{
Regex exStr = new Regex(@"^\d+$");
if (exStr.Match(str).Success)
return true;
else
return false;
}
#endregion
作者:sy
出处:http://www.cnblogs.com/sydeveloper
QQ:522733724
本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利