using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Data.Common;
using System.Text.RegularExpressions;
using System.Reflection;
using System.IO;
namespace Sql_Function
{
/// <summary>
/// 数据处理底层 数据基本操作
/// </summary>
public class SqlDb
{
#region 数据库公用连接字符串
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
#endregion
#region 数据库操作方法 连接和关闭对象
/// <summary>
/// 打开数据库连接
/// </summary>
/// <returns></returns>
public static SqlConnection OpenDataBase()
{
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
conn.Open();
return conn;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
/// <param></param>
public static void CloseDataBase(SqlConnection conn)
{
try
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
}
catch (Exception ex)
{
conn.Close();
conn.Dispose();
throw new Exception(ex.Message);
//throw new ArgumentNullException("connection");
}
finally
{
conn.Close();
}
}
#endregion
#region 数据库返回DataTable
/// <summary>
/// 返回指定Sql语句的返回DataTable
/// </summary>
/// <param>传入的Sql语句</param>
/// <returns>DataTable</returns>
public static DataTable ReturnDataTable(string strSQL)
{
SqlConnection conn = OpenDataBase();
DataTable table = new DataTable();
SqlDataAdapter da = null;
try
{
da = new SqlDataAdapter(strSQL, conn);
da.Fill(table);
da.Dispose();
CloseDataBase(conn);
return table;
}
catch (Exception e)
{
da.Dispose();
table.Dispose();
CloseDataBase(conn);
throw new Exception(e.Message);
}
finally
{
da.Dispose();
CloseDataBase(conn);
}
}
/// <summary>
/// 返回DataTable记录集,并且实现分页功能
/// </summary>
/// <param>传入SQL语句</param>
/// <param>当前页</param>
/// <param>每页分页大小</param>
/// <param>填充数据库表名称</param>
/// <returns></returns>
public static DataTable ReturnDataTablePage(string sqls, int currentpage, int pagesize, string table)
{
SqlConnection conn = OpenDataBase();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqls, conn);
try
{
int startcount;
if (currentpage < 1)
{
startcount = currentpage * pagesize;
}
else
{
startcount = (currentpage - 1) * pagesize;
}
da.Fill(ds, startcount, pagesize, table);
da.Dispose();
CloseDataBase(conn);
return ds.Tables[0];
}
catch (Exception ex)
{
da.Dispose();
CloseDataBase(conn);
ds.Dispose();
throw new Exception(ex.Message);
}
finally
{
da.Dispose();
CloseDataBase(conn);
}
}
#endregion
#region 数据库公用方法集
/// <summary>
/// 返回记录总数 传入带有count(主键)统计的Sql语句
/// </summary>
/// <param>传入带有count(主键)统计的Sql语句</param>
/// <returns></returns>
public static string RecordCounts(string StrSql)
{
string ProcInfo = "0";
SqlConnection conn = OpenDataBase();
SqlCommand comm = new SqlCommand(StrSql, conn);
comm.CommandTimeout = 120;
SqlDataReader DataReaders = comm.ExecuteReader(); //返回值
if (DataReaders.Read())
{
ProcInfo = DataReaders[0].ToString();
}
DataReaders.Close();
comm.Dispose();
CloseDataBase(conn);
return ProcInfo;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Sql_Function
{
public class PublicFunction
{
/// <summary>
/// 判断是否为数字 TRUE代表不是数字,False代表是数字
/// </summary>
/// <param>字符串</param>
/// <returns></returns>
public static bool isNumber(string s)
{
int Flag = 0;
char[] str = s.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str[i]))
{
Flag++;
}
else
{
Flag = -1;
break;
}
}
if (Flag > 0)
{
return false;
}
else
{
return true;
}
}
}
}
前台页面样例:
复制代码 代码如下: