using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; using System.Security.Cryptography; using System.Text; using System.IO; /// ///common 的摘要说明 /// public class common { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString()); #region 修改产品浏览次数 /// /// 修改浏览次数 /// /// public bool count(int id) { string sqlstr = " update v3_merchandises set See_count=See_count +1 where PID = " + id; if (ExcuteNoneQuery(sqlstr, null)) { return true; } else return false; } #endregion #region 修改产品购买次数 /// /// 修改产品购买数量 /// /// /// public bool countbuy(int id) { string sqlstr = " update v3_merchandises set Buy_Count =Buy_Count +1 where PID = " + id; if (ExcuteNoneQuery(sqlstr, null)) { return true; } else return false; } #endregion #region 修改产品浏览次数 /// /// 修改文章浏览 次数 /// /// /// /// public bool count(int id, string Name, string TableName) { string sqlstr = " update " + TableName + " set " + Name + "=" + Name + "+1 where = " + id; if (ExcuteNoneQuery(sqlstr, null)) { return true; } else return false; } #endregion #region 执行传入的sql语句(可以带参数),并返回。 可以执行 增、删、改 /// /// 执行传入的sql语句(可以带参数),并返回。可以执行 增、删、改 /// /// 传入的sql语句 /// 参数,以SqlParameter[]的形式传入 /// 如果成功,返回真,否则返回假 public bool ExcuteNoneQuery(string sqlstr, SqlParameter[] par) { SqlCommand com = new SqlCommand(sqlstr, con); com.Connection.Open(); if (par != null) { com.Parameters.AddRange(par); } int result = com.ExecuteNonQuery(); com.Connection.Close(); return result > 0 ? true : false; } #endregion #region 获取数据,返回DataSet,无参数 /// /// 获取数据,返回DataSet,无参数 /// /// sql语句 /// 如果ds不等于空,则返回ds,否则返空 public DataSet ExcuteReader(string sqlstr) { SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con); DataSet ds = new DataSet(); sda.Fill(ds); return ds != null ? ds : null; } #endregion #region 比较两个字符串是否相等 /// /// 比较两个字符串是否相等 /// /// /// /// 两个字符串相等返回真,否则返回假 public bool Equels(string str1, string str2) { if (str1 != null || str2 != null) { if (str1.Equals(str2)) { return true; } else { return false; } } return false; } #endregion #region 判断数据库中是否存在值 public bool ExecuteScalar(string sqlstr, SqlParameter[] par) { SqlCommand com = new SqlCommand(sqlstr, con); com.Connection.Open(); bool b = Convert.ToInt32(com.ExecuteScalar()) > 0 ? true : false; com.Connection.Close(); return b; } #endregion /// /// 对字符串进行加密 /// /// 要加密的字符串 /// 8位工作密钥 /// 加密后的字符串 public static string Encode(string str, string key) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } stream.Close(); return builder.ToString(); } /// /// 对字符串进行解密 /// /// 要解密的字符串 /// 8位工作密钥 /// 解密后的字符串 public static string Decode(string str, string key) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] buffer = new byte[str.Length / 2]; for (int i = 0; i < (str.Length / 2); i++) { int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num2; } MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); stream.Close(); return Encoding.GetEncoding("GB2312").GetString(stream.ToArray()); } /// /// 清除指定Cookie /// /// cookiename public static void ClearCookie(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; if (cookie != null) { //cookie.Expires = DateTime.Now.AddYears(-33); //HttpContext.Current.Response.Cookies.Add(cookie); cookie.Expires = DateTime.Now.AddDays(-200); HttpContext.Current.Response.Cookies.Set(cookie); //HttpContext.Current.Request.Cookies.Remove(cookiename); } } /// /// 获取指定Cookie值 /// /// cookiename /// public static string GetCookieValue(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; string str = string.Empty; if (cookie != null) { str = cookie.Value; } return str; } /// /// 添加一个Cookie(3个月过期) /// /// Cookie名 /// Cookie值 public static void SetCookie(string cookiename, string cookievalue) { SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(15.0)); } /// /// 添加一个Cookie,设置过期时间 /// /// cookie名 /// cookie值 /// 过期时间 DateTime public static void SetCookie(string cookiename, string cookievalue, DateTime expires) { HttpCookie cookie = new HttpCookie(cookiename) { Value = cookievalue, Expires = expires }; HttpContext.Current.Response.Cookies.Add(cookie); } }