C# Access数据库操作

5 篇文章 1 订阅

从Access数据库中读取数据,用TreeView显示数据名称列表:

using System.Data.OleDb;       //引用命名空间
string sql1 = "select * from 客户";//编写sql语句
string sql2 = "select * from 订单";
System.Data.OleDb.OleDbConnection OleDbConnection1 = new System.Data.OleDb.OleDbConnection();

//OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data //Source=F:\\SQL\\Sources\\Northwind.mdb;";//适用于2003版本Access

OleDbConnection1.ConnectionString = @"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=F:\\SQL\\Sources\\Northwind.mdb;";  //新建数据库连接,适用于2007版本Access

OleDbConnection1.Open();

System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(sql1, OleDbConnection1);

System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter2 = new System.Data.OleDb.OleDbDataAdapter(sql2, OleDbConnection1);

OleDbDataAdapter1.Fill(DataSet1, "客户");    //使用OleDbCommand类来执行Sql语句

OleDbDataAdapter2.Fill(DataSet1, "订单");

TreeNode Root = new TreeNode("Northwind 数据库");

TreeView1.Nodes.Add(Root);               //显示数据名称列表

//循环查找数据集中的表对象
foreach (DataTable Table in DataSet1.Tables)  
{
    TreeNode n1 = new TreeNode(Table.TableName);
    //循环查找每个表中的列对象
    foreach (DataColumn column in DataSet1.Tables[Table.TableName].Columns)
    {

        //将表中的字段名加入到每个表的节点下
        n1.Nodes.Add(column.ColumnName);
    }

    //将数据集中的所有表名加入到reeView的根节点下
    Root.Nodes.Add(n1);
}
OleDbConnection1.Close();               //关闭数据库连接

ACCESS 连接,查询,编辑,更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;  

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

转载地址:C#操作Access数据库步骤_米兔by-CSDN博客_c#access数据库操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值