现在的位置: 首页 > 综合 > 正文

SQLHelper收集(二)

2012年02月16日 ⁄ 综合 ⁄ 共 14819字 ⁄ 字号 评论关闭
 

  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. using System.Data.SqlClient;
  11. using System.Web.Configuration;
  12. using System.Web.SessionState;
  13. /// <summary>
  14. /// DBClass 的摘要说明
  15. /// </summary>
  16. public class DBClass
  17. {
  18.     public static SqlConnection myConnection;
  19.     public static SqlCommand myCommand;
  20.     public static  DataSet ds;
  21.     public static DataTable dt;
  22.     public static DataRow dr;
  23.     public static SqlDataAdapter myAdapter;
  24.     public static SqlDataAdapter myAdapter1;
  25.     public static SqlDataReader myReader;
  26.     protected static HttpResponse Response;
  27.     protected static HttpSessionState Session;
  28.     public DBClass()
  29.     {
  30.         //
  31.         // TODO: 在此处添加构造函数逻辑
  32.         //
  33.     }
  34.     /*
  35.      * 打开数据库的连接用力判断数据库是否处于关闭或打开状态
  36.      * 采用webconfig的连接机制。 
  37.      * */
  38.     #region 打开数据库操作
  39.     public static void Open()
  40.     {
  41.       string strConnection;
  42.         if (myConnection == null)
  43.         {
  44.             //strConnection = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  45.             myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
  46.             myConnection.Open();
  47.         }
  48.         else
  49.         {
  50.             if (myConnection.State == ConnectionState.Closed)
  51.             {
  52.                 if (myConnection.ConnectionString == "" || myConnection.ConnectionString == null)
  53.                 {
  54.                     strConnection = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  55.                     myConnection.ConnectionString = strConnection;
  56.                 }
  57.                 myConnection.Open();
  58.             }
  59.         }
  60.     }
  61.     #endregion 关闭数据库操作
  62.     /*
  63.          * 关闭数据库的连接状态,清除dataset中的数据
  64.          */
  65.     #region 关闭数据库
  66.     public static  void Close()
  67.     {
  68.         if (ds != null)
  69.         {
  70.             ds.Clear();
  71.             ds.Dispose();
  72.         }
  73.         if (myConnection.State == ConnectionState.Open)
  74.         {
  75.             try
  76.             {
  77.                 myAdapter.Dispose();
  78.             }
  79.             catch (Exception)
  80.             {
  81.             }
  82.             myConnection.Close();
  83.             myConnection.Dispose();
  84.         }
  85.     }
  86.     /*
  87.      *关闭数据连接,清除数据适配器中的数据,取消填充数据
  88.      * 
  89.      */
  90.     public static void CloseConnection()
  91.     {
  92.         if (myConnection.State == ConnectionState.Open)
  93.         {
  94.             try
  95.             {
  96.                 myAdapter.Dispose();
  97.             }
  98.             catch (Exception)
  99.             {
  100.             }
  101.             myConnection.Close();
  102.             myConnection.Dispose();
  103.         }
  104.     }
  105.     #endregion
  106.     # region 释放资源
  107.     public static  void Dispose()
  108.     {
  109.         if (ds != null)
  110.         {
  111.             ds.Dispose();
  112.         }
  113.         if (myAdapter != null)
  114.         {
  115.             myAdapter.Dispose();
  116.         }
  117.         if (myAdapter1 != null)
  118.         {
  119.             myAdapter.Dispose();
  120.         }
  121.         if (myCommand != null)
  122.         {
  123.             myCommand.Dispose();
  124.         }
  125.         if (myReader != null)
  126.         {
  127.             if (myReader.IsClosed)
  128.             {
  129.                 myReader.Close();
  130.             }
  131.         }
  132.     }
  133.     #endregion 
  134.     //sqlconnetion的方法用于数据的连接
  135.     public static SqlConnection getconn()
  136.     {
  137.         SqlConnection conn;
  138.         string connections = System.Configuration.ConfigurationSettings.AppSettings["Connectionstring"];
  139.         conn = new SqlConnection(connections);
  140.         return conn;
  141.     }
  142.     //对数据库执行sql操作
  143.     public static SqlCommand getCommand()
  144.     {
  145.         SqlCommand myCmd = new SqlCommand();
  146.         return myCmd;
  147.     }
  148.     public static SqlCommand getCommand(string strArg)
  149.     {
  150.         SqlCommand myCmd = new SqlCommand(strArg);
  151.         return myCmd;
  152.     }
  153.     public static SqlCommand getCommand(string strArg, SqlConnection connArg)
  154.     {
  155.         Open();
  156.         SqlCommand myCmd = new SqlCommand(strArg, connArg);
  157.         return myCmd;
  158.     }
  159.     //执行 Transact-SQL INSERT、DELELE、UPDATE 及 SET 语句等命令。
  160.     public static  void ExeSql(string str_Sql)
  161.     {
  162.         if (myConnection.State == ConnectionState.Closed)
  163.         {
  164.             Open();
  165.         }
  166.         myCommand = new SqlCommand(str_Sql, myConnection);
  167.         myCommand.ExecuteNonQuery();
  168.         myCommand.Dispose();
  169.         CloseConnection();
  170.     }
  171.     //读取数据
  172.     public static void Fill(string str_Sql)
  173.     {
  174.         myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
  175.         if (myConnection.State == ConnectionState.Closed)
  176.         {
  177.             Open();
  178.         }
  179.         myAdapter = new SqlDataAdapter(str_Sql, myConnection);
  180.         ds = new DataSet();
  181.         myAdapter.Fill(ds);
  182.     }
  183.     public static void FillAdd(string tabname, string str_Sql)
  184.     {
  185.         myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
  186.         if (myConnection.State == ConnectionState.Closed)
  187.         {
  188.             Open();
  189.         }
  190.         myAdapter = new SqlDataAdapter(str_Sql, myConnection);
  191.         if (ds == null)
  192.             ds = new DataSet();
  193.         myAdapter.Fill(ds, tabname);
  194.     }
  195.     //dataset的数据绑定
  196.     public static int GetRowCount(string str_Sql)
  197.     {
  198.         int intCount;
  199.         Fill(str_Sql);
  200.         if (ds.Tables.Count < 1)
  201.         {
  202.             Close();
  203.             intCount = 0;
  204.         }
  205.         else
  206.         {
  207.             intCount = ds.Tables[0].Rows.Count;
  208.         }
  209.         return intCount;
  210.     }
  211.     public static void GetRowRecord(string str_Sql)
  212.     {
  213.         Fill(str_Sql);
  214.         if (ds.Tables.Count > 0)
  215.         {
  216.             if (ds.Tables[0].Rows.Count > 0)
  217.             {
  218.                 dr = ds.Tables[0].Rows[0];
  219.             }
  220.         }
  221.         myConnection.Close();
  222.         myConnection.Dispose();
  223.     }
  224.     /*
  225.  * 服务器控件数据的绑定
  226.  * */
  227.     public static void BindDataList(string sql, DataList mydatalist)
  228.     {
  229.         Fill(sql);
  230.         mydatalist.DataSource = ds.Tables[0].DefaultView;
  231.         mydatalist.DataBind();
  232.     }
  233.     public static void BindRepeater(string sql, Repeater myrepeater)
  234.     {
  235.         Fill(sql);
  236.         myrepeater.DataSource = ds.Tables[0].DefaultView;
  237.         myrepeater.DataBind();
  238.     }
  239.     //datagrid 的数据库多种绑定
  240.     /// <summary>
  241.     /// 获取DataGrid被选中的项索引若没有选中,返回-1
  242.     /// </summary>
  243.     /// <param name="DG">要操作的DataGrid</param>
  244.     /// <returns>选中的项索引,没有选中返回-1</returns>
  245.     public static int SelectedIndex(DataGrid DG)
  246.     {
  247.         if (DG.SelectedIndex == -1)
  248.         {
  249.             return -1;
  250.         }
  251.         else
  252.             return DG.SelectedIndex;
  253.     }
  254.     /// <summary>
  255.     /// 获取DataGrid被选中的项的DataKeys值,若没有选中,提示
  256.     /// 若DataGrid没有设置DataKeyField,提示 
  257.     /// </summary>
  258.     /// <param name="DG">要操作的DataGrid</param>
  259.     /// <returns>选中的项DataKeys值</returns>
  260.     public static string SelectedCode(DataGrid DG)
  261.     {
  262.         string thisCode = null;
  263.         if (DG.Items.Count == 0)
  264.         {
  265.             WriteMessage("目前没有数据,请添加!"truetrue);
  266.             return null;
  267.         }
  268.         if (SelectedIndex(DG) == -1)
  269.         {
  270.             WriteMessage("请选择一项!"truetrue);
  271.             return null;
  272.         }
  273.         else
  274.         {
  275.             if (DG.DataKeyField.ToString().Trim().Length != 0)
  276.             {
  277.                 thisCode = DG.DataKeys[SelectedIndex(DG)].ToString().Trim();
  278.                 return thisCode;
  279.             }
  280.             else
  281.             {
  282.                 WriteMessage("DataGrid没有设置DataKeyField,因此无法确定选择项!"truetrue);
  283.                 return null;
  284.             }
  285.         }
  286.     }
  287.     public static void BindDataGrid(string strsql, DataGrid dg)
  288.     {
  289.         try
  290.         {
  291.             Open();
  292.             Fill(strsql);
  293.             dg.DataSource = ds.Tables[0].DefaultView;
  294.             if (dg.CurrentPageIndex > dg.PageCount - 1)
  295.             {
  296.                 if (dg.PageCount > 0)
  297.                 {
  298.                     dg.CurrentPageIndex = dg.PageCount - 1;
  299.                 }
  300.                 else
  301.                 {
  302.                     dg.CurrentPageIndex = 0;
  303.                 }
  304.             }
  305.             dg.DataBind();
  306.         }
  307.         catch (Exception e)
  308.         {
  309.             WriteMessage(e.Message.ToString().Trim(), truetrue);
  310.         }
  311.         finally
  312.         {
  313.             Close();
  314.             Dispose();
  315.         }
  316.     }
  317.     public static void BindDataGrid1(string sql, DataGrid mydg)
  318.     {
  319.         Fill(sql);
  320.         mydg.DataSource = ds.Tables[0].DefaultView;
  321.         mydg.DataBind();
  322.     }
  323.     //若datagrid有多页,而被删除项处于第n(n>1)页且该页只有1条数据时,删除后会导致页数减去1,再次绑定datagrid时
  324.     //仍然定位到该页引发错误.
  325.     //故定义此函数防止此中错误出现
  326.     //执行删除语句后,再次绑定前,调用此函数即可
  327.     public static void DeleteDgNotice(DataGrid dg)
  328.     {
  329.         if ((dg.Items.Count % dg.PageSize == 1) && (dg.PageCount > 1))
  330.         {
  331.             if (dg.PageCount > 1)
  332.             {
  333.                 dg.CurrentPageIndex = dg.CurrentPageIndex - 1;
  334.             }
  335.             else
  336.             {
  337.                 dg.CurrentPageIndex = 0;
  338.             }
  339.         }
  340.     }
  341.     //利用sql语句绑定给定的datagrid
  342.     //并将其中一列进行UrlEndcode 以作为超链接的参数
  343.     //使用该函数时 ,将datagrid超链接的url字段处填写"keyfield"即可
  344.     //strql表示sql语句
  345.     //dg表示给定的datagrid
  346.     //columnname表示需要UrlEncode的列名
  347.     public static void BindDataGrid(string strsql, DataGrid dg, string ColumnName)
  348.     {
  349.         try
  350.         {
  351.             if (myConnection.State == ConnectionState.Closed)
  352.             {
  353.                 Open();
  354.             }
  355.             Fill(strsql);
  356.             ds.Tables[0].Columns.Add("KeyField");
  357.             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  358.             {
  359.                 ds.Tables[0].Rows[i]["KeyField"] = HttpUtility.UrlDecode(ds.Tables[0].Rows[i][ColumnName].ToString().Trim());
  360.                 dg.DataSource = ds.Tables[0].DefaultView;
  361.                 if (dg.CurrentPageIndex > dg.PageCount - 1)
  362.                 {
  363.                     if (dg.PageCount > 0)
  364.                     {
  365.                         dg.CurrentPageIndex = dg.PageCount - 1;
  366.                     }
  367.                     else
  368.                     {
  369.                         dg.CurrentPageIndex = 0;
  370.                     }
  371.                 }
  372.             }
  373.             dg.DataBind();
  374.         }
  375.         catch (Exception e)
  376.         {
  377.             WriteMessage(e.Message.ToString().Trim(), truetrue);
  378.         }
  379.         finally
  380.         {
  381.             Close();
  382.             Dispose();
  383.         }
  384.     }
  385.     public static void BindDropDownList(string str_Text, string sql, DropDownList myDropDownList)
  386.     {
  387.         // 绑定DropDownList控件(注:四个函数,该函数需要一个字段名,分别绑定Value和Text两值,默认表名)
  388.         //Open();
  389.         if (ds != null)
  390.         {
  391.             FillAdd("binddropdownlist" + sql.ToString(), sql);
  392.             myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
  393.         }
  394.         else
  395.         {
  396.             Fill(sql);
  397.             myDropDownList.DataSource = ds.Tables[0].DefaultView;
  398.         }
  399.         myDropDownList.DataValueField = str_Text;
  400.         myDropDownList.DataTextField = str_Text;
  401.         myDropDownList.DataBind();
  402.     }
  403.     /// <summary>
  404.     /// 绑定DropDownList控件并显示数据,DropDownList控件Value,Text值将分别等于等于str_Value,str_Text值
  405.     /// </summary>
  406.     /// <param name="str_Value">绑定DropDownList控件Value值相对应数据库表字段名</param>
  407.     /// <param name="str_Text">绑定DropDownList控件Text值相对应数据库表字段名</param>
  408.     /// <param name="sql">Select-SQL语句</param>
  409.     /// <param name="myDropDownList">DropDownList控件id值</param>
  410.     public static void BindDropDownList(string str_Value, string str_Text, string sql, DropDownList myDropDownList)
  411.     {
  412.         try
  413.         {
  414.             if (myConnection.State == ConnectionState.Closed)
  415.             {
  416.                 Open();
  417.             }
  418.             if (ds != null)
  419.             {
  420.                 FillAdd("binddropdownlist" + sql.ToString(), sql);
  421.                 myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
  422.             }
  423.             else
  424.             {
  425.                 Fill(sql);
  426.                 myDropDownList.DataSource = ds.Tables[0].DefaultView;
  427.             }
  428.             myDropDownList.DataValueField = str_Value;
  429.             myDropDownList.DataTextField = str_Text;
  430.             myDropDownList.DataBind();
  431.             if (myDropDownList.Items.Count == 0)
  432.             {
  433.                 ListItem li_null = new ListItem("无""无");
  434.                 myDropDownList.Items.Add(li_null);
  435.             }
  436.         }
  437.         catch (Exception e)
  438.         {
  439.             WriteMessage(e.Message.ToString().Trim(), truetrue);
  440.         }
  441.         finally
  442.         {
  443.             Close();
  444.             Dispose();
  445.         }
  446.     }
  447.     public static void BindDropDownList(string str_Value, string str_Text, string sql, DropDownList myDropDownList, bool all)
  448.     {
  449.         if (ds != null)
  450.         {
  451.             FillAdd("binddropdownlist" + sql.ToString(), sql);
  452.             if (all)
  453.             {
  454.                 DataRow drL = ds.Tables["binddropdownlist" + sql.ToString()].NewRow();
  455.                 drL[str_Text] = "";
  456.                 drL[str_Value] = "";
  457.                 ds.Tables["binddropdownlist" + sql.ToString()].Rows.InsertAt(drL, 0);
  458.             }
  459.             myDropDownList.DataSource = ds.Tables["binddropdownlist" + sql.ToString()].DefaultView;
  460.         }
  461.         else
  462.         {
  463.             Fill(sql);
  464.             if (all)
  465.             {
  466.                 DataRow drL = ds.Tables[0].NewRow();
  467.                 drL[str_Text] = "";
  468.                 drL[str_Value] = "";
  469.                 ds.Tables[0].Rows.InsertAt(drL, 0);
  470.             }
  471.             myDropDownList.DataSource = ds.Tables[0].DefaultView;
  472.         }
  473.         myDropDownList.DataValueField = str_Value;
  474.         myDropDownList.DataTextField = str_Text;
  475.         myDropDownList.DataBind();
  476.         //Close();
  477.     }
  478.     /// <summary>
  479.     /// 绑定DropDownList控件,取得选中值
  480.     /// </summary>
  481.     /// <param name="str_Value">数据库表示Value值字段</param>
  482.     /// <param name="str_Text">数据库表示Text值字段</param>
  483.     /// <param name="str_Value_Field">选中项目的值</param>
  484.     /// <param name="str_Sql">绑定数据的SQL语句</param>
  485.     /// <param name="myDropDownList">下拉列表框的名称</param>
  486.     public static void SelectBindDropDownListValue(string str_Value, string str_Text, string str_Value_Field, string str_Sql, DropDownList myDropDownList)
  487.     {
  488.         BindDropDownList(str_Value, str_Text, str_Sql, myDropDownList);// 绑定myDropDownList控件
  489.         myDropDownList.Items[0].Selected = false;
  490.         for (int i = 0; i < myDropDownList.Items.Count; i++)
  491.         {
  492.             if (str_Value_Field == myDropDownList.Items[i].Value)
  493.             {
  494.                 myDropDownList.Items[i].Selected = true;
  495.                 break;
  496.             }
  497.         }
  498.     }
  499.     //以javascript的windous.alert()方法输出提示信息
  500.     //strmsg 表示要输出的信息
  501.     //back 表示输出后是否要history.back()
  502.     //end 表示输出后是否要Response.End()
  503.     public static void WriteMessage(string strMsg, bool Back, bool End)
  504.     {
  505.         Response = HttpContext.Current.Response;
  506.         //将单引号替换,防止js出错
  507.         strMsg = strMsg.Replace("'""");
  508.         //将回车符号换掉,防止js出错
  509.         strMsg = strMsg.Replace("/r/n""");
  510.         Response.Write("<script language=javascript>alert('" + strMsg + "')</script>");
  511.         if (Back)
  512.         {
  513.             Response.Write("<script language=javascript>history.back();</script)");
  514.         }
  515.         if (End)
  516.         {
  517.             Response.End();
  518.         }
  519.     }
  520. }

抱歉!评论已关闭.