欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#(.NET)數(shù)據(jù)訪問(wèn)連接、查詢、插入等操作的封裝類

 更新時(shí)間:2008年05月30日 19:29:26   作者:  
一個(gè)C#(.NET)數(shù)據(jù)訪問(wèn)連接、查詢、插入等操作的封裝類
using System;
using System.Data;
using System.Data.SqlClient;
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;

/// <summary>
/// Public 的摘要說(shuō)明
/// </summary>
public class PublicClass
{   //定義一個(gè)公用成員
    public SqlConnection conn;

    public PublicClass()
    {
        //
        // TODO: 在此處添加構(gòu)造函數(shù)邏輯
        //
    }
    #region 建立數(shù)據(jù)庫(kù)連接
    public void OpenConn()
    {
        String strconn = System.Configuration.ConfigurationManager.AppSettings["sqlconn"].ToString();
        conn = new SqlConnection(strconn);
        if (conn.State.ToString().ToLower() == "open")
        {
            //連接為打開時(shí)
        }
        else
        {
            //連接為關(guān)閉時(shí)
            conn.Open();
        }
    }
    #endregion
    #region 關(guān)閉并釋放連接
    public void CloseConn()
    {
        if (conn.State.ToString().ToLower() == "open")
        {
            //連接為打開時(shí)
            conn.Close();
            conn.Dispose();
        }
    }
    #endregion
    #region 返回DataReader,用于讀取數(shù)據(jù)
    public SqlDataReader DataRead(string sql)
    {
        OpenConn();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader dr = cmd.ExecuteReader();
        return dr;
    }
#endregion
    #region 返回一個(gè)數(shù)據(jù)集
    public DataSet MySqlDataSet(string Sql, string tableName)
    {
        OpenConn();
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(Sql, conn);
        da.Fill(ds, tableName);
        CloseConn();
        return ds;
    }
    #endregion
    //返回一個(gè)數(shù)據(jù)集
    public DataView MySqlDataSource(string Sql)
    {
        OpenConn();
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(Sql, conn);
        da.Fill(ds, "temp");
        CloseConn();
        return ds.Tables[0].DefaultView;
    }
    #region 執(zhí)行一個(gè)SQL操作:添加、刪除、更新操作

    //執(zhí)行一個(gè)SQL操作:添加、刪除、更新操作
    public void MySqlExcute(string sql)
    {
        OpenConn();
        SqlCommand cmd;
        cmd = new SqlCommand(sql, conn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        CloseConn();
    }
    #endregion
    #region 執(zhí)行一個(gè)SQL操作:添加、刪除、更新操作,返回受影響的行
    //執(zhí)行一個(gè)SQL操作:添加、刪除、更新操作,返回受影響的行
    public int MySqlExecuteNonQuery(string sql)
    {
        OpenConn();
        SqlCommand cmd;
        cmd = new SqlCommand(sql, conn);
        int flag = cmd.ExecuteNonQuery();
        return flag;
    }
    #endregion

    public object MySqlExecuteScalar(string sql)
    {
        OpenConn();
        SqlCommand cmd;
        cmd = new SqlCommand(sql, conn);
        object obj = cmd.ExecuteScalar();
        cmd.Dispose();
        CloseConn();
        return obj;
    }

    /// <summary>
    /// 返回DataTable對(duì)象
    /// </summary>
    /// <param name="sql">sql語(yǔ)句</param>
    /// <returns></returns>
    public DataTable MySqlDataTable(string sql)
    {
        OpenConn();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        da.Fill(ds, "table");
        CloseConn();
        return ds.Tables["table"];
    }

    /// <summary>
    /// 返回一個(gè)數(shù)據(jù)集的記錄數(shù)
    /// </summary>
    /// <param name="sql">傳遞的sql語(yǔ)句必須為一個(gè)統(tǒng)計(jì)查詢</param>
    /// <returns></returns>
    public int MySqlRecordCount(string sql)
    {
        //注:Sql 語(yǔ)句必須是一個(gè)統(tǒng)計(jì)查詢
        OpenConn();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = sql;
        cmd.Connection = conn;
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        int RecordCount = -1;
        while (dr.Read())
        {
            RecordCount = int.Parse(dr[0].ToString());
        }
        CloseConn();
        return RecordCount;
    }

    /// <summary>
    /// 自定義的功能警告
    /// </summary>
    /// <param name="str">彈出信息框內(nèi)容</param>
    public void SetAlert(string str)
    {
        HttpContext.Current.Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('" + str + "');</script>");

    }
    //返回上一頁(yè)
    public void AddErro(string message)
    {
        HttpContext.Current.Response.Write("<script>alert('" + message + "');history.back(-1);</script>");
    }


    //關(guān)閉窗口
    public void SetCloseWindow()
    {
        HttpContext.Current.Response.Write("<script language='JavaScript' type='text/JavaScript'>window.close();</script>");
    }

    /// <summary>
    /// 地址跳轉(zhuǎn)
    /// </summary>
    /// <param name="str">跳轉(zhuǎn)地址</param>
    public void SetLocation(string str)
    {
        HttpContext.Current.Response.Write("<script language='JavaScript' type='text/JavaScript'>location='" + str + "';</script>");
    }


    public string AjaxSetAlert(string str)
    {
        return "<script language='JavaScript' type='text/JavaScript'>alert('" + str + "');</script>";
    }

    //過(guò)濾非法字符
    public string FilterStr(string Str)
    {
        Str = Str.Trim();
        Str = Str.Replace("*", "");
        Str = Str.Replace("=", "");
        Str = Str.Replace("/", "");
        Str = Str.Replace("$", "");
        Str = Str.Replace("#", "");
        Str = Str.Replace("@", "");
        Str = Str.Replace("&", "");
        return Str;
    }

    //Md5加密算法
    public string md5(string str)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower().Substring(0, 12);
    }
    public string RndNum(int VcodeNum)
    {
        string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,W,X";
        string[] VcArray = Vchar.Split(new Char[] { ',' }); //將字符串生成數(shù)組
        string VNum = "";
        int temp = -1;

        Random rand = new Random();

        for (int i = 1; i < VcodeNum + 1; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }

            int t = rand.Next(31);            //數(shù)組一般從0開始讀取,所以這里為31*Rnd
            if (temp != -1 && temp == t)
            {
                return RndNum(VcodeNum);
            }
            temp = t;
            VNum += VcArray[t];
        }
        return VNum;
    }
}

相關(guān)文章

  • ASP.NET Core自動(dòng)生成小寫破折號(hào)路由的實(shí)現(xiàn)方法

    ASP.NET Core自動(dòng)生成小寫破折號(hào)路由的實(shí)現(xiàn)方法

    這篇文章主要介紹了ASP.NET Core自動(dòng)生成小寫破折號(hào)路由的實(shí)現(xiàn)方法,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下
    2021-04-04
  • .Net語(yǔ)言Smobiler開發(fā)之如何在手機(jī)上實(shí)現(xiàn)表單設(shè)計(jì)

    .Net語(yǔ)言Smobiler開發(fā)之如何在手機(jī)上實(shí)現(xiàn)表單設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了.Net語(yǔ)言APP開發(fā)平臺(tái),如何在手機(jī)上實(shí)現(xiàn)表單設(shè)計(jì)(Smobiler),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • DropDownList 下拉框選擇改變促發(fā)事件和防全局刷新(推薦)

    DropDownList 下拉框選擇改變促發(fā)事件和防全局刷新(推薦)

    這篇文章主要介紹了DropDownList 下拉框選擇改變促發(fā)事件和防全局刷新(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10
  • C#可以減少或不使用switch有什么方法

    C#可以減少或不使用switch有什么方法

    減少或不使用switch,大家有什么好的方法,使用工廠方法來(lái)處理,可以創(chuàng)建一個(gè)工廠接口,然后每個(gè)方法設(shè)計(jì)為一個(gè)工廠類,并實(shí)現(xiàn)工廠接口,感興趣的朋友可以了解下
    2013-03-03
  • 基于ASP.NET實(shí)現(xiàn)單點(diǎn)登錄(SSO)的示例代碼

    基于ASP.NET實(shí)現(xiàn)單點(diǎn)登錄(SSO)的示例代碼

    SSO英文全稱Single?Sign?On(單點(diǎn)登錄)。SSO是在多個(gè)應(yīng)用系統(tǒng)中,用戶只需要登錄一次就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng)。本文為大家分享了基于ASP.NET實(shí)現(xiàn)單點(diǎn)登錄(SSO)的示例代碼,需要的可以參考一下
    2022-05-05
  • asp DataTable添加列和行的三種方法

    asp DataTable添加列和行的三種方法

    DataTable添加列和行的方法,大家可以參考下下面的代碼。
    2009-12-12
  • .Net程序防止被注入代碼(整站通用)分享

    .Net程序防止被注入代碼(整站通用)分享

    防止sql注入,通常一個(gè)一個(gè)文件修改不僅麻煩而且還有漏掉的危險(xiǎn),下面我說(shuō)一上如何從整個(gè)系統(tǒng)防止注入
    2013-11-11
  • ASP.NET Lable中進(jìn)行換行解決方案

    ASP.NET Lable中進(jìn)行換行解決方案

    這個(gè)問(wèn)題,應(yīng)該算是很簡(jiǎn)單的問(wèn)題,可說(shuō)實(shí)在的,折騰了2個(gè)小時(shí)的時(shí)候,后面整出來(lái)的時(shí)候,真坑爹呢?,F(xiàn)在把這個(gè)過(guò)程給大家,希望可以給大家一個(gè)提醒
    2012-12-12
  • Element NavMenu導(dǎo)航菜單的使用方法

    Element NavMenu導(dǎo)航菜單的使用方法

    這篇文章主要介紹了Element NavMenu導(dǎo)航菜單的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • VS2015中C#版本6.0的新特性 你需要知道

    VS2015中C#版本6.0的新特性 你需要知道

    VS2015內(nèi)置的C#版本為6.0,提供了一些新特性,這篇文章主要為大家詳細(xì)介紹了VS2015中C#版本6.0的8種新特性,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評(píng)論