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
    }
相关推荐
远歌已逝13 分钟前
维护在线重做日志(二)
数据库·oracle
qq_433099401 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz1 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10222 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
工业甲酰苯胺3 小时前
Redis性能优化的18招
数据库·redis·性能优化
没书读了4 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
----云烟----4 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024064 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎