c# 操作Microsoft Access数据库

数据库结构为:

cs 复制代码
public static string connting = "数据库路径:如:D:\\xxx.mdb";

//插入
  public bool InsertToFile(string casenumber, int lastrowid, int pagecount)
        {

 bool result = true;

 try
            {
                string connString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={connting};";
                using (var conn = new OleDbConnection(connString))
                {
                    conn.Open();
                    var sql = @"INSERT INTO [file] (
        [顺序号], [案卷号], [案卷题名], [卷内页数文件], [卷内页数图纸], [卷内页数其他],[备注]) VALUES (
        @value1, @value2, @value3, @value4, @value5, @value6,
        @value7)";
                    using (var cmd = new OleDbCommand(sql, conn))
                    {
                        cmd.Parameters.Add(new OleDbParameter("@value1", OleDbType.Integer) { Value = 1 });
                        cmd.Parameters.Add(new OleDbParameter("@value2", OleDbType.VarChar) { Value = casenumber });
                        cmd.Parameters.Add(new OleDbParameter("@value3", OleDbType.VarChar) { Value = "目录索引" });
                        cmd.Parameters.Add(new OleDbParameter("@value4", OleDbType.Integer) { Value = pagecount });
                        cmd.Parameters.Add(new OleDbParameter("@value5", OleDbType.Integer) { Value = 0 });
                        cmd.Parameters.Add(new OleDbParameter("@value6", OleDbType.VarChar) { Value = "" });
                        cmd.Parameters.Add(new OleDbParameter("@value7", OleDbType.VarChar) { Value = "" });
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                result = false;
                Logger.Error("插入[file]表异常!详情:" + ex.Message);
            }
            return result;


}

//查询
public void QueryData()
{
    try
    {
        string connString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={connting};";
        using (var conn = new OleDbConnection(connString))
        {
            conn.Open();
            var sql = "SELECT * FROM [file] WHERE [顺序号] = @orderNumber";
            using (var cmd = new OleDbCommand(sql, conn))
            {
                cmd.Parameters.Add(new OleDbParameter("@orderNumber", OleDbType.Integer) { Value = 1 });
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // 处理查询结果
                        string casenumber = reader.GetString(reader.GetOrdinal("案卷号"));
                        string title = reader.GetString(reader.GetOrdinal("案卷题名"));
                        // 其他列的处理类似
                        
                        Console.WriteLine($"案卷号: {casenumber}, 案卷题名: {title}");
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error("查询[file]表异常!详情:" + ex.Message);
    }
}

//修改
public bool UpdateData(string casenumber, int pagecount)
{
    bool result = true;
    try
    {
        string connString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={connting};";
        using (var conn = new OleDbConnection(connString))
        {
            conn.Open();
            var sql = "UPDATE [file] SET [卷内页数文件] = @pagecount WHERE [案卷号] = @casenumber";
            using (var cmd = new OleDbCommand(sql, conn))
            {
                cmd.Parameters.Add(new OleDbParameter("@pagecount", OleDbType.Integer) { Value = pagecount });
                cmd.Parameters.Add(new OleDbParameter("@casenumber", OleDbType.VarChar) { Value = casenumber });
                int rowsAffected = cmd.ExecuteNonQuery();
                if (rowsAffected == 0)
                {
                    result = false;
                    Logger.Error("未找到符合条件的记录!");
                }
            }
        }
    }
    catch (Exception ex)
    {
        result = false;
        Logger.Error("更新[file]表异常!详情:" + ex.Message);
    }
    return result;
}

这里需要注意的点就是字段类型,注意代码中的类型和数据库类型保持一致,否则插入会异常

枚举为:

cs 复制代码
public enum OleDbType
    {
        BigInt = 20,
        Binary = 0x80,
        Boolean = 11,
        BSTR = 8,
        Char = 129,
        Currency = 6,
        Date = 7,
        DBDate = 133,
        DBTime = 134,
        DBTimeStamp = 135,
        Decimal = 14,
        Double = 5,
        Empty = 0,
        Error = 10,
        Filetime = 0x40,
        Guid = 72,
        IDispatch = 9,
        Integer = 3,
        IUnknown = 13,
        LongVarBinary = 205,
        LongVarChar = 201,
        LongVarWChar = 203,
        Numeric = 131,
        PropVariant = 138,
        Single = 4,
        SmallInt = 2,
        TinyInt = 0x10,
        UnsignedBigInt = 21,
        UnsignedInt = 19,
        UnsignedSmallInt = 18,
        UnsignedTinyInt = 17,
        VarBinary = 204,
        VarChar = 200,
        Variant = 12,
        VarNumeric = 139,
        VarWChar = 202,
        WChar = 130
    }
相关推荐
CoderIsArt1 小时前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
△曉風殘月〆1 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
励志成为嵌入式工程师3 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
逐·風3 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
师太,答应老衲吧3 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
Channing Lewis4 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce