SqlData.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:3k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- //添加引用空间
- using System.Data.SqlClient;
- /// <summary>
- /// SqlData 的摘要说明
- /// </summary>
- public class SqlData
- {
- public SqlData()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- /// <summary>
- /// 连接数据库
- /// </summary>
- /// <returns></returns>
- public SqlConnection ExceCon()
- {
- SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSQL"]);
- Con.Open();
- return Con;
- }
- #region
- /// <summary>
- /// 绑定GridView控件
- /// </summary>
- /// <param name="dl">要绑定的GridView控件</param>
- /// <param name="cmdtxt">要执行的SQL语句</param>
- /// <param name="tblName">绑定的数据表名</param>
- /// <returns></returns>
- public bool BindData(GridView dl, string cmdtxt,string tblName)
- {
- dl.DataSource = this.ExceDS(cmdtxt,tblName);
- try
- {
- dl.DataBind();
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- ExceCon().Close();
- }
- }
- #endregion
- #region
- /// <summary>
- /// 返回一个DataSet数据类型的数据
- /// </summary>
- /// <param name="cmdtxt">要执行的SQL语句</param>
- /// <param name="tblName">要绑定的数据表</param>
- /// <returns></returns>
- public DataSet ExceDS(string cmdtxt,string tblName)
- {
- SqlConnection Con = ExceCon();
- SqlCommand Com;
- DataSet ds=null;
- try
- {
- Com = new SqlCommand(cmdtxt, Con);
- SqlDataAdapter Da = new SqlDataAdapter();
- Da.SelectCommand = Com;
- ds = new DataSet(tblName);
- Da.Fill(ds);
- }
- catch (Exception ex)
- {
- Con.Close();
- }
- return ds;
- }
- #endregion
- #region
- /// <summary>
- /// 执行SQL语句
- /// </summary>
- /// <param name="cmdtxt">要执行的SQL语句</param>
- /// <returns></returns>
- public bool ExceSQL(string cmdtxt)
- {
- SqlCommand Com = new SqlCommand(cmdtxt, ExceCon());
- try
- {
- Com.ExecuteNonQuery();
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- ExceCon().Close();
- }
- }
- #endregion
- /// <summary>
- /// 返回SqlDataReader数据类型
- /// </summary>
- /// <param name="cmdtxt">要执行的SQL语句</param>
- /// <returns></returns>
- public SqlDataReader ExceDr(string cmdtxt)
- {
- SqlCommand Com = new SqlCommand(cmdtxt, ExceCon());
- SqlDataReader dr = Com.ExecuteReader();
- return dr;
- }
- }