ASP.NET常用的一些数据操作类
/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
public static SqlConnection conn=new SqlConnection();
public static SqlCommand cmd=new SqlCommand();
public DataAccess()
{
//
/
/ TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 创建并返回数据库连接对象
/// </summary>
/// <returns></returns>
public static SqlConnection CreateConn()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ToString());
return conn;
}
/// <summary>
/// 打开数据库连接
/// </summary>
public static void Open()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
cmd.Connection = conn;
conn.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public static void Closed()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDataTable(string sql)
{
try
{
Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
catch (Exception Ex)
{
throw new Exception(Ex.Message,Ex);
}
}
/
// <summary>
/// 返回DataSet
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet GetDataSet(string sql)
{
try
{
Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
Closed();
}
catch (Exception Ex)
{
throw new Exception(Ex.Message,Ex);
}
}
/// <summary>
/
// 执行sql语句返回整数值进行判断
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExcSql(string sql)
{
try
{
Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
return 1;
Closed();
}
catch (Exception Ex)
{
return -1;
Closed();
}
}
/// <summary>
/
// 执行sql语句返回执行结果字符串
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static string ExSql(string sql)
{
try
{
Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
return cmd.ExecuteScalar().ToString();
Closed();
}
catch (Exception Ex)
{
string Errmess = "数据查询错误!";
return Errmess;
}
}
/// <summary>
/// 统计记录数
/
// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int CountCmd(string sql)
{
try
{
Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
return (int)cmd.ExecuteScalar();
}
catch (Exception Ex)
{
return -1;
}
}
/// <summary>
/// 截取字符串长度
asp数据/// </summary>
/// <param name="Str"></param>
/// <param name="Len"></param>
/
// <returns></returns>
public static string LeftStr(string Str, int Len)
{
string Strs = "";
if (Str.Length > Len)
{
Strs = Str.Substring(0, Len) + "...";
return Strs;
}
else
{
Strs = Str;
return Strs;
}
}
/// <summary>
/// 判断用户登录
/// </summary>
/// <param name="username"></param>
/// <param name="Pwd"></param>
/// <returns></returns>
public static bool CheckLogin(string username, string Pwd)
{
Open();
string sql = "select count(*) from [User] where UserName='"+username+"' and UserPwd='"+Pwd+"'";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
string logincount = "update [User] set LoginCount=LoginCount+1,IP='" + HttpContext.Current.Request.UserHostAddress + "',Stats=1 where UserName='" + username + "'";
ExcSql(logincount);//向数据库加入用户登录信息
return true;
Closed();
}
else
{
return false;
Closed();
}
}
/// <summary>
/// 获取系统参数
/// </summary>
/
// <param name="Str"></param>
/// <returns></returns>
public static string System(string Str)
{
Open();
try
{
string sql = "select " + Str + " from [System]";
DataTable dt = GetDataTable(sql);
string Strs = dt.Rows[0][Str].ToString();
return Strs;
Closed();
}
catch (Exception Ex)
{
throw new Exception(Ex.Message,Ex);
}
}
/// <summary>
/// 获取随机密码
/// </summary>
/// <returns></returns>
public static string MakePwd()
{
string Strs = "abcdefghijklmnopqrstuvwxyz1234567890";
int len = 12;
string Pwd = "";
int num;
Random rnd = new Random();
for (int i = 0; i < len; i++)
{
num = rnd.Next(Strs.Length);
Pwd+=Strs[num];
}
return Pwd;
}
/// <summary>
/// 判断管理员是否登录
/// </summary>
public static void IsAdmin()
{
if (HttpContext.Current.Session["Admin"] == null)
{
HttpContext.Current.Response.Write("<script>alert('您还没有登录或登录超时,请重新登录!');location.href='login.aspx';</script>");
}
}
}
<---------------------------------------------------End 自己写的常用的----------------------------------------------------------->
<--------下面是最近写个留言本用的------------>
public static OleDbConnection con()
{//数据库连接类
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["book"].ConnectionString);
return con;
}
public static bool insert(string que)
{ //根据传进来的SQL语句执行插入操作
OleDbConnection con = ();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
return true;
else
return false;
}
public static DataTable ds(string que)
{//返回一个装载了SQL制定留言的数据表,
OleDbConnection con = ();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand=new OleDbCommand(que,con);
DataSet ds = new DataSet();
oda.Fill(ds,"thc");
return ds.Tables["thc"];
}
public static bool img(string que)
{//根据传来的条件查询该项是否有内容,有就返回true
OleDbConnection con = ();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
if (cmd.ExecuteScalar().ToString() != "")
return true;
else
return false;
}
public static string scr(string que)
{//同样是根据传来的SQL语句返回一个字段的值,我不太喜欢把SQL语句做在内中,感觉不灵活
OleDbConnection con = ();
con.Open();
OleDbCommand cmd = new OleDbCommand(que,con);
return cmd.ExecuteScalar().ToString();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论