首頁 資料庫 mysql教程 .net连接ACCESS数据库,网页上不停的刷新就报错

.net连接ACCESS数据库,网页上不停的刷新就报错

Jun 07, 2016 pm 03:43 PM
.net access 重新整理 報錯 資料庫 網頁 連接

public class DB { public static OleDbConnection Conn; public static string ConnString;//连接字符串 public DB() { // // TODO: 在此处添加构造函数逻辑 // } public static OleDbConnection Getconn() { ConnString = "Provider=Microsoft.Jet.OLEDB.4

public class DB
{
    public static OleDbConnection Conn;
    public static string ConnString;//连接字符串

    public DB()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public static OleDbConnection Getconn()
    {
        ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        Conn = new OleDbConnection(ConnString);
        //if (Conn.State.Equals(ConnectionState.Closed))
        //{

        //    Conn.Open();

        //}

        if (Conn == null)
        {
            Conn = new OleDbConnection(ConnString);
            Conn.Open();
        }
        else if (Conn.State == System.Data.ConnectionState.Closed)
        {
            Conn.Open();
        }
        else if (Conn.State == System.Data.ConnectionState.Broken)
        {
            Conn.Close();
            Conn.Open();
        }
        return Conn;

    }
    //=================================================
    //功能描述:关闭数据库
    //时间:2010.11.10
    //=================================================
    private static void closeConnection()
    {
        OleDbConnection conn = DB.Getconn();
        OleDbCommand cmd = new OleDbCommand();
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
    }
    //=================================================
    //功能描述:执行SQL语句
    //输入参数:sql,查询的SQL语句
    //时间:2010.11.10
    //=================================================
    public static void execnonsql(string sql)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            OleDbCommand com = new OleDbCommand(sql, conn);
            com.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }

    }
    //=================================================
    //功能描述:获取DATASET
    //输入参数:sql,查询的SQL语句
    //返回值:DataSet
    //时间:2010.11.10
    //=================================================
    public static DataSet getdataset(string sql)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "ds");
            return ds;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:获取DATASET1
    //输入参数:sql,查询的SQL语句
    //返回值:DataSet
    //时间:2010.11.10
    //=================================================
    public static DataSet select(string sql, string tablename)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, tablename);
            return ds;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:获取某个字段数据
    //输入参数:sql,查询的SQL语句
    //返回值:hang
    //时间:2010.11.10
    //=================================================
    public static string FindString(string sql)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            OleDbCommand com = new OleDbCommand(sql, conn);
            string hang = Convert.ToString(com.ExecuteScalar());
            return hang;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }

    }
    //=================================================
    //功能描述:对DATAGRIG进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void binddatagrid(string sql, DataGrid dg)
    {

        try
        {
            DataSet ds = getdataset(sql);
            dg.DataSource = ds.Tables[0].DefaultView;
            dg.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对DropDownList进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void bindDropDownList(string sql, DropDownList dl, string class_name, string id)
    {

        try
        {
            DataSet ds = getdataset(sql);
            dl.DataSource = ds.Tables[0].DefaultView;
            dl.DataTextField = class_name;
            dl.DataValueField = id;
            dl.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对RadioButtonList进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void bindRadioButtonList(string sql, RadioButtonList rl, string class_name, string id)
    {

        try
        {
            DataSet ds = getdataset(sql);
            rl.DataSource = ds.Tables[0].DefaultView;
            rl.DataTextField = class_name;
            rl.DataValueField = id;
            rl.SelectedIndex = 0;
            rl.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对GridView进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void bindGridView(string sql, GridView dg)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            DataSet ds = getdataset(sql);
            dg.DataSource = ds.Tables[0].DefaultView;
            dg.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对datalist进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dl,需要绑定的datalist控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void binddatalist(string sql, DataList dl)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            DataSet ds = getdataset(sql);
            dl.DataSource = ds.Tables[0].DefaultView;
            dl.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对repeater进行数据绑定,无排序
    //输入参数:sql,查询的SQL语句;dl,需要绑定的repeater控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void bindrepeater(string sql, Repeater rp)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            DataSet ds = getdataset(sql);
            rp.DataSource = ds.Tables[0].DefaultView;
            rp.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    //=================================================
    //功能描述:对listbox进行数据绑定
    //输入参数:sql,查询的SQL语句;listb,需要绑定的listbox控件
    //返回值:无
    //时间:2010.11.10
    //=================================================
    public static void bindlistbox(string sql, ListBox listb, string class_name, string id)
    {
        try
        {
            closeConnection();
            OleDbConnection conn = DB.Getconn();
            DataSet ds = getdataset(sql);
            listb.DataSource = ds.Tables[0].DefaultView;
            listb.DataTextField = class_name;
            listb.DataValueField = id;
            listb.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);

        }
        finally
        {
            closeConnection();
        }
    }
    ///


    /// 返回 HTML 字符串的编码结果
    ///

    /// 字符串
    /// 编码结果
    public static string HtmlEncode(string str)
    {
        return HttpUtility.HtmlEncode(str);
    }

    ///


    /// 返回 HTML 字符串的解码结果
    ///

    /// 字符串
    /// 解码结果
    public static string HtmlDecode(string str)
    {
        return HttpUtility.HtmlDecode(str);
    }
    ///
    /// 检测是否有Sql危险字符
    ///

    /// 要判断字符串
    /// 判断结果
    public static bool IsSafeSqlString(string str)
    {

        return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
    }
    ///


    /// 检测用户登录。
    ///

    ///
    ///
    public static string UserCheck(string username, string userpass)
    {
        string strsql = "select count(*) from Member where mem_Name='" + username + "' and mem_Password='" + userpass + "'";
        OleDbConnection conn = DB.Getconn();
        OleDbCommand com = new OleDbCommand(strsql, conn);
        string hang = Convert.ToString(com.ExecuteScalar());
        return hang;
    }

}

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1253
24
MySQL與其他數據庫:比較選項 MySQL與其他數據庫:比較選項 Apr 15, 2025 am 12:08 AM

MySQL適合Web應用和內容管理系統,因其開源、高性能和易用性而受歡迎。 1)與PostgreSQL相比,MySQL在簡單查詢和高並發讀操作上表現更好。 2)相較Oracle,MySQL因開源和低成本更受中小企業青睞。 3)對比MicrosoftSQLServer,MySQL更適合跨平台應用。 4)與MongoDB不同,MySQL更適用於結構化數據和事務處理。

甲骨文在商業世界中的作用 甲骨文在商業世界中的作用 Apr 23, 2025 am 12:01 AM

Oracle不僅是數據庫公司,還是雲計算和ERP系統的領導者。 1.Oracle提供從數據庫到雲服務和ERP系統的全面解決方案。 2.OracleCloud挑戰AWS和Azure,提供IaaS、PaaS和SaaS服務。 3.Oracle的ERP系統如E-BusinessSuite和FusionApplications幫助企業優化運營。

MySQL:結構化數據和關係數據庫 MySQL:結構化數據和關係數據庫 Apr 18, 2025 am 12:22 AM

MySQL通過表結構和SQL查詢高效管理結構化數據,並通過外鍵實現表間關係。 1.創建表時定義數據格式和類型。 2.使用外鍵建立表間關係。 3.通過索引和查詢優化提高性能。 4.定期備份和監控數據庫確保數據安全和性能優化。

使用DICR/YII2-Google將Google API集成在YII2中 使用DICR/YII2-Google將Google API集成在YII2中 Apr 18, 2025 am 11:54 AM

vProcesserazrabotkiveb被固定,мнелостольностьстьс粹餾標д都LeavallySumballanceFriablanceFaumDoptoMatification,Čtookazalovnetakprosto,kakaožidal.posenesko

nginx限流怎麼解決 nginx限流怎麼解決 Apr 14, 2025 pm 12:06 PM

Nginx 限流問題可通過以下方法解決:使用 ngx_http_limit_req_module 限制請求次數;使用 ngx_http_limit_conn_module 限制連接數;使用第三方模塊(ngx_http_limit_connections_module、ngx_http_limit_rate_module、ngx_http_access_module)實現更多限流策略;使用雲服務(Cloudflare、Google Cloud Rate Limiting、AWS WAF)進行 DD

c#.net的持續相關性:查看當前用法 c#.net的持續相關性:查看當前用法 Apr 16, 2025 am 12:07 AM

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

從網絡到桌面:C#.NET的多功能性 從網絡到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

MySQL:一種對數據存儲的初學者友好方法 MySQL:一種對數據存儲的初學者友好方法 Apr 17, 2025 am 12:21 AM

MySQL適合初學者,因為它易用且功能強大。 1.MySQL是關係型數據庫,使用SQL進行CRUD操作。 2.安裝簡單,需配置root用戶密碼。 3.使用INSERT、UPDATE、DELETE、SELECT進行數據操作。 4.複雜查詢可使用ORDERBY、WHERE和JOIN。 5.調試需檢查語法,使用EXPLAIN分析查詢。 6.優化建議包括使用索引、選擇合適數據類型和良好編程習慣。

See all articles