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

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

Jun 07, 2016 pm 03:43 PM
.net access refresh Report an error database Web page connect

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;
    }

}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

How to solve nginx current limit How to solve nginx current limit Apr 14, 2025 pm 12:06 PM

The Nginx current limit problem can be solved by: use ngx_http_limit_req_module to limit the number of requests; use ngx_http_limit_conn_module to limit the number of connections; use third-party modules (ngx_http_limit_connections_module, ngx_http_limit_rate_module, ngx_http_access_module) to implement more current limit policies; use cloud services (Cloudflare, Google Cloud Rate Limiting, AWS WAF) to DD

From Web to Desktop: The Versatility of C# .NET From Web to Desktop: The Versatility of 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: A Beginner-Friendly Approach to Data Storage MySQL: A Beginner-Friendly Approach to Data Storage Apr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL: Key Features and Capabilities Explained MySQL: Key Features and Capabilities Explained Apr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

See all articles