c# 操作Microsoft Access数据库

数据库结构为:


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

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

枚举为:

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
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值