zoukankan      html  css  js  c++  java
  • Access操作类

    3     C#代码操作步骤

    (1)   命名空间

                using System.Data.OleDb;

    (2)   连接字符串

                private staticstring connStr = @"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:login.accdb";

                注意:

                 a、DataSource = 数据库存放的路径(这里数据库放到了D盘目录下)

                 b、 2003版本的Access数据库链接字符串:  privatestatic stringconnStr =@"Provider = Microsoft.Jet.OLEDB.4.0;DataSource = d:login.mdb";

                       2007版本的Access数据库链接字符串:privatestaticstring connStr =@"Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:login.accdb";

    (3)   建立连接:

                OleDbConnection tempconn =new OleDbConnection(connStr);

    (4)   使用OleDbCommand类来执行Sql语句:

               OleDbCommand cmd = new OleDbCommand(sql, tempconn);  

               tempconn.Open();

               cmd.ExecuteNonQuery();

    (5)   代码

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Data.OleDb;
    
    using System.Windows.Forms;
    
    using System.Data;
    
     
    
    namespace test
    
    {
    
        class AccessHelper
    
        {
    
            private static string connStr = @"Provider = Microsoft.Ace.OLEDB.12.0;Data Source = d:login.accdb";
    
     
    
            public static OleDbConnection GetConn()
    
            {
    
                OleDbConnection tempconn = new OleDbConnection(connStr);
    
                MessageBox.Show(tempconn.DataSource);
    
                tempconn.Open();
    
                MessageBox.Show(tempconn.State.ToString());
    
                return (tempconn);
    
            }
    
     
    
            /// <summary>
    
            /// 执行增加、删除、修改指令
    
            /// </summary>
    
            /// <param name="sql">增加、删除、修改的sql语句</param>
    
            /// <param name="param">sql语句的参数</param>
    
            /// <returns></returns>
    
            public static int ExecuteNonQuery(string sql, params OleDbParameter[] param)
    
            {
    
                using (OleDbConnection conn = new OleDbConnection(connStr))
    
                {
    
                    using (OleDbCommand cmd = new OleDbCommand(sql,conn))
    
                    {
    
                        if (param != null)
    
                        {
    
                            cmd.Parameters.AddRange(param);
    
                        }
    
                        conn.Open();
    
                        return(cmd.ExecuteNonQuery());
    
                    }
    
                }
    
            }
    
     
    
            /// <summary>
    
            /// 执行查询指令,获取返回的首行首列的值
    
            /// </summary>
    
            /// <param name="sql">查询sql语句</param>
    
            /// <param name="param">sql语句的参数</param>
    
            /// <returns></returns>
    
            public static object ExecuteScalar(string sql, params OleDbParameter[] param)
    
            {
    
                using (OleDbConnection conn = new OleDbConnection(connStr))
    
                {
    
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
    
                    {
    
                        if (param != null)
    
                        {
    
                            cmd.Parameters.AddRange(param);
    
                        }
    
                        conn.Open();
    
                        return (cmd.ExecuteScalar());
    
                    }
    
                }
    
            }
    
     
    
            /// <summary>
    
            /// 执行查询指令,获取返回的datareader
    
            /// </summary>
    
            /// <param name="sql">查询sql语句</param>
    
            /// <param name="param">sql语句的参数</param>
    
            /// <returns></returns>
    
            public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param)
    
            {
    
                OleDbConnection conn = new OleDbConnection(connStr);
    
                OleDbCommand cmd = conn.CreateCommand();
    
                cmd.CommandText = sql;
    
                cmd.CommandType = CommandType.Text;
    
                if (param != null)
    
                {
    
                    cmd.Parameters.AddRange(param);
    
                }
    
                conn.Open();
    
                return (cmd.ExecuteReader(CommandBehavior.CloseConnection));
    
     
    
            }
    
     
    
            /// <summary>
    
            /// 执行查询指令,获取返回datatable
    
            /// </summary>
    
            /// <param name="sql">查询sql语句</param>
    
            /// <param name="param">sql语句的参数</param>
    
            /// <returns></returns>
    
            public static DataTable ExecuteDatable(string sql, params OleDbParameter[] param)
    
            {
    
                using (OleDbConnection conn = new OleDbConnection(connStr))
    
                {
    
                    using (OleDbCommand cmd = new OleDbCommand(sql, conn))
    
                    {
    
                        if (param != null)
    
                        {
    
                            cmd.Parameters.AddRange(param);
    
                        }
    
                        DataTable dt = new DataTable();
    
                        OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
    
                        sda.Fill(dt);
    
                        return (dt);
    
                    }
    
                }
    
     
    
            }
    
        }
    
    }
    

      

  • 相关阅读:
    最大并发连接数和最大会话数的区别
    Redis Sentinel 情况下bind地址设置
    ZooKeeper 授权验证
    推荐一个zookeeper信息查看工具
    WebForm-博客园-6.0-空间(Space)-短信息(Msg)
    ylbtech-cnblogs(博客园)-数据库设计-6.0-Msg(短消息)
    WebForm-博客园-1.0-账户模块(Passport)-登录与注册
    WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
    ylbtech-cnblogs(博客园)-数据库设计-1,Passport(账户)
    IIS 配置
  • 原文地址:https://www.cnblogs.com/wanggc/p/4654285.html
Copyright © 2011-2022 走看看