数据库结构为:
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
}