c# 操作Microsoft Access数据库
数据库结构为:https://img-blog.csdnimg.cn/direct/dad6aa80dd8c4b44850be4ff8e1b1145.png
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 (
[顺序号], [案卷号], [案卷题名], [卷内页数文件], [卷内页数图纸], [卷内页数其他],[备注]) 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("插入表异常!详情:" + 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 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("查询表异常!详情:" + 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 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("更新表异常!详情:" + 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
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]