SqlData.cs
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:3k
源码类别:

Email客户端

开发平台:

Visual C++

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. //添加引用空间
  11. using System.Data.SqlClient;
  12. /// <summary>
  13. /// SqlData 的摘要说明
  14. /// </summary>
  15. public class SqlData
  16. {
  17. public SqlData()
  18. {
  19. //
  20. // TODO: 在此处添加构造函数逻辑
  21. //
  22. }
  23.     /// <summary>
  24.     /// 连接数据库
  25.     /// </summary>
  26.     /// <returns></returns>
  27.     public SqlConnection ExceCon()
  28.     {
  29.         SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSQL"]);
  30.         Con.Open();
  31.         return Con;
  32.     }
  33.     #region
  34.     /// <summary>
  35.     /// 绑定GridView控件
  36.     /// </summary>
  37.     /// <param name="dl">要绑定的GridView控件</param>
  38.     /// <param name="cmdtxt">要执行的SQL语句</param>
  39.     /// <param name="tblName">绑定的数据表名</param>
  40.     /// <returns></returns>
  41.     public bool BindData(GridView dl, string cmdtxt,string tblName)
  42.     {
  43.         dl.DataSource = this.ExceDS(cmdtxt,tblName);
  44.         try
  45.         {
  46.             dl.DataBind();
  47.             return true;
  48.         }
  49.         catch
  50.         {
  51.             return false;
  52.         }
  53.         finally
  54.         {
  55.             ExceCon().Close();
  56.         }
  57.     }
  58.     #endregion
  59.     #region
  60.     /// <summary>
  61.     /// 返回一个DataSet数据类型的数据
  62.     /// </summary>
  63.     /// <param name="cmdtxt">要执行的SQL语句</param>
  64.     /// <param name="tblName">要绑定的数据表</param>
  65.     /// <returns></returns>
  66.     public DataSet ExceDS(string cmdtxt,string tblName)
  67.     {
  68.         SqlConnection Con = ExceCon();
  69.         SqlCommand Com;
  70.         DataSet ds=null;
  71.         try
  72.         {
  73.             Com = new SqlCommand(cmdtxt, Con);
  74.             SqlDataAdapter Da = new SqlDataAdapter();
  75.             Da.SelectCommand = Com;
  76.             ds = new DataSet(tblName);
  77.             Da.Fill(ds);
  78.         }
  79.         catch (Exception ex)
  80.         {
  81.             Con.Close();
  82.         }
  83.         return ds;
  84.     }
  85.     #endregion
  86.     #region
  87.     /// <summary>
  88.     /// 执行SQL语句
  89.     /// </summary>
  90.     /// <param name="cmdtxt">要执行的SQL语句</param>
  91.     /// <returns></returns>
  92.     public bool ExceSQL(string cmdtxt)
  93.     {
  94.         SqlCommand Com = new SqlCommand(cmdtxt, ExceCon());
  95.         try
  96.         {
  97.             Com.ExecuteNonQuery();
  98.             return true;
  99.         }
  100.         catch
  101.         {
  102.             return false;
  103.         }
  104.         finally
  105.         {
  106.             ExceCon().Close();
  107.         }
  108.     }
  109.     #endregion
  110.     /// <summary>
  111.     /// 返回SqlDataReader数据类型
  112.     /// </summary>
  113.     /// <param name="cmdtxt">要执行的SQL语句</param>
  114.     /// <returns></returns>
  115.     public SqlDataReader ExceDr(string cmdtxt)
  116.     {
  117.         SqlCommand Com = new SqlCommand(cmdtxt, ExceCon());
  118.         SqlDataReader dr = Com.ExecuteReader();
  119.         return dr;
  120.     }
  121. }