CSFramework.DB多数据底层类库 - 操作对象数据DbDataUpdate接口测试
CSFramework.DB多数据底层类库 - 操作对象数据DbDataUpdate接口测试CSFramework.DB简介
我们在开发软件过程中,特别是基于数据管理应用相关的系统,一般采用一种数据库,如Microsoft SQL Server,超大型系统有的使用Oracle,部分Web系统采用开源的MySQL等,由于各种业务场景以及用户需求,促使我们的数据库系统部署在不同类型的数据库服务器上。若开发的系统能支持多种数据库的快速切换,可以为我们减少很多烦恼,同时提高系统的适应性、兼容性以及可扩展性。
扫一扫加微信
我们在开发软件过程中,特别是基于数据管理应用相关的系统,一般采用一种数据库,如Microsoft SQL Server,超大型系统有的使用Oracle,部分Web系统采用开源的MySQL等,由于各种业务场景以及用户需求,促使我们的数据库系统部署在不同类型的数据库服务器上。若开发的系统能支持多种数据库的快速切换,可以为我们减少很多烦恼,同时提高系统的适应性、兼容性以及可扩展性。
CSFramework.DB数据库底层应用框架
CSFramework.DB测试程序主界面:
调用 DbDataUpdate.Update()方法 提交DataTable对象。
C# Code:
//
//测试案例:提交DataTable,批量修改提交多条记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
DbCommandGenerator gen = new DbCommandGenerator(db);
var dt = db.GetTable("SELECT TOP 10 * FROM dbo.sys_RegisterLog");
dt.Rows[0]["HID"] = "修改HID";//修改一条记录
dt.Rows[1].Delete();//删除一条记录
var row = dt.Rows.Add();//新增一条记录
row["LicenseID"] = "xxxx";
row["RegisterNo"] = "xxxx";
row["RegisterTime"] = DateTime.Now;
row["LogConent"] = "xxxx";
row["HID"] = "xxxx";
//根据Model提交数据
int i = new DbDataUpdate(db).Update(dt, typeof(sys_RegisterLog), new string[] { "isid" }, new string[] { "isid" });
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:提交DataTable,批量修改提交多条记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
DbCommandGenerator gen = new DbCommandGenerator(db);
var dt = db.GetTable("SELECT TOP 10 * FROM dbo.sys_RegisterLog");
dt.Rows[0]["HID"] = "修改HID";//修改一条记录
dt.Rows[1].Delete();//删除一条记录
var row = dt.Rows.Add();//新增一条记录
row["LicenseID"] = "xxxx";
row["RegisterNo"] = "xxxx";
row["RegisterTime"] = DateTime.Now;
row["LogConent"] = "xxxx";
row["HID"] = "xxxx";
//根据Model提交数据
int i = new DbDataUpdate(db).Update(dt, typeof(sys_RegisterLog), new string[] { "isid" }, new string[] { "isid" });
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.Update()方法 提交DataSet对象(手动模式启动事务):
C# Code:
//
//测试案例:提交DataSet,批量修改、提交多表多条记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
//表1
var dt = db.GetTable("SELECT TOP 10 * FROM dbo.sys_RegisterLog ORDER BY isid ASC", "sys_RegisterLog");
dt.Rows[0]["HID"] = "修改HID";//修改一条记录
dt.Rows[1].Delete();//删除一条记录
var row = dt.Rows.Add();//新增一条记录
row["LicenseID"] = "xxxx";
row["RegisterNo"] = "xxxx";
row["RegisterTime"] = DateTime.Now;
row["LogConent"] = "xxxx";
row["HID"] = "xxxx";
var row2 = dt.Rows.Add();//新增一条记录
row2["LicenseID"] = "xxxx22";
row2["RegisterNo"] = "xxxx22";
row2["RegisterTime"] = DateTime.Now;
row2["LogConent"] = "xxxx22";
row2["HID"] = "xxxx22";
//表2
var dt1 = db.GetTable("SELECT TOP 10 * FROM dbo.sys_AdminUser ORDER BY isid DESC", "sys_AdminUser");
var row1 = dt1.Rows[0];
row1["AccountName"] = row1["AccountName"].ToString() + "-1";
row1["LastLoginTime"] = DateTime.Now;//修改数据
//
//重要!!! 构建ORM对象模型与表结构的关系
//sys_RegisterLog表模型关系
//sys_AdminUser表模型关系
List<DbDataUpdateORM> orms = new List<DbDataUpdateORM>();
orms.Add(new DbDataUpdateORM(typeof(sys_RegisterLog), new string[] { "isid" }, new string[] { "isid" }));
orms.Add(new DbDataUpdateORM(typeof(sys_AdminUser), new string[] { "Account" }, new string[] { "isid" }));
//组合DataSet, 一次性提交多张表
DataSet ds = new DataSet();
ds.Tables.Add(dt.GetChanges());
ds.Tables.Add(dt1.GetChanges());
//提交数据
int i = 0;
DbTransaction tran = db.TransBegin();
DbDataUpdate updater = new DbDataUpdate(db, tran);
try
{
i = updater.Update(ds, orms);//提交数据
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
ShowResult(ex.Message);
}
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:提交DataSet,批量修改、提交多表多条记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
//表1
var dt = db.GetTable("SELECT TOP 10 * FROM dbo.sys_RegisterLog ORDER BY isid ASC", "sys_RegisterLog");
dt.Rows[0]["HID"] = "修改HID";//修改一条记录
dt.Rows[1].Delete();//删除一条记录
var row = dt.Rows.Add();//新增一条记录
row["LicenseID"] = "xxxx";
row["RegisterNo"] = "xxxx";
row["RegisterTime"] = DateTime.Now;
row["LogConent"] = "xxxx";
row["HID"] = "xxxx";
var row2 = dt.Rows.Add();//新增一条记录
row2["LicenseID"] = "xxxx22";
row2["RegisterNo"] = "xxxx22";
row2["RegisterTime"] = DateTime.Now;
row2["LogConent"] = "xxxx22";
row2["HID"] = "xxxx22";
//表2
var dt1 = db.GetTable("SELECT TOP 10 * FROM dbo.sys_AdminUser ORDER BY isid DESC", "sys_AdminUser");
var row1 = dt1.Rows[0];
row1["AccountName"] = row1["AccountName"].ToString() + "-1";
row1["LastLoginTime"] = DateTime.Now;//修改数据
//
//重要!!! 构建ORM对象模型与表结构的关系
//sys_RegisterLog表模型关系
//sys_AdminUser表模型关系
List<DbDataUpdateORM> orms = new List<DbDataUpdateORM>();
orms.Add(new DbDataUpdateORM(typeof(sys_RegisterLog), new string[] { "isid" }, new string[] { "isid" }));
orms.Add(new DbDataUpdateORM(typeof(sys_AdminUser), new string[] { "Account" }, new string[] { "isid" }));
//组合DataSet, 一次性提交多张表
DataSet ds = new DataSet();
ds.Tables.Add(dt.GetChanges());
ds.Tables.Add(dt1.GetChanges());
//提交数据
int i = 0;
DbTransaction tran = db.TransBegin();
DbDataUpdate updater = new DbDataUpdate(db, tran);
try
{
i = updater.Update(ds, orms);//提交数据
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
ShowResult(ex.Message);
}
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.AddObject()方法,新增一个对象到数据库:
C# Code:
//
//测试案例:新增一个对象,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user0 = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmss"),
AccountName = "test",
AccountType = "xx",
Password = "pwd"
};
//新增一个对象
bool b = new DbDataUpdate(db).AddObject(user0, new string[] { "isid" });
ShowResult($"更新{b}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:新增一个对象,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user0 = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmss"),
AccountName = "test",
AccountType = "xx",
Password = "pwd"
};
//新增一个对象
bool b = new DbDataUpdate(db).AddObject(user0, new string[] { "isid" });
ShowResult($"更新{b}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.AddList<>()方法,新增多个对象到数据库:
C# Code:
//
//测试案例:新增对象列表,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmssfff"),
AccountName = "test",
AccountType = "xx",
Password = "pwd"
};
Thread.Sleep(50);
sys_AdminUser user2 = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmssfff"),
AccountName = "test2",
AccountType = "xx",
Password = "pwd"
};
List<sys_AdminUser> list = new List<sys_AdminUser>();
list.Add(user);
list.Add(user2);
//提交数据
int i = 0;
DbTransaction tran = db.TransBegin();
DbDataUpdate updater = new DbDataUpdate(db, tran);
try
{
i = updater.AddList<sys_AdminUser>(list, new string[] { "isid" });
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
ShowResult(ex.Message);
}
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:新增对象列表,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmssfff"),
AccountName = "test",
AccountType = "xx",
Password = "pwd"
};
Thread.Sleep(50);
sys_AdminUser user2 = new sys_AdminUser
{
Account = "Test-" + DateTime.Now.ToString("yyMMddHHmmssfff"),
AccountName = "test2",
AccountType = "xx",
Password = "pwd"
};
List<sys_AdminUser> list = new List<sys_AdminUser>();
list.Add(user);
list.Add(user2);
//提交数据
int i = 0;
DbTransaction tran = db.TransBegin();
DbDataUpdate updater = new DbDataUpdate(db, tran);
try
{
i = updater.AddList<sys_AdminUser>(list, new string[] { "isid" });
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
ShowResult(ex.Message);
}
ShowResult($"共更新记录数:{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.UpdateObject()方法,更新对象数据:
C# Code:
//
//测试案例:修改对象,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_RegisterLog log = db.ExecuteReader<sys_RegisterLog>("SELECT * FROM dbo.sys_RegisterLog where isid=133");
log.HID = "xxxxxxxxxxxxxxxxxxxx";
log.LicenseID = "asdfasdfasf";
log.LogConent = "asfdasdfasdfasdfasdf";
log.RegisterNo = "xxxxxxxxxxxxxxxxx";
log.RegisterTime = DateTime.Now;
//修改对象
bool ok = new DbDataUpdate(db).UpdateObject(log, new string[] { "isid" }, new string[] { "isid" });
ShowResult($"操作{ok}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:修改对象,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_RegisterLog log = db.ExecuteReader<sys_RegisterLog>("SELECT * FROM dbo.sys_RegisterLog where isid=133");
log.HID = "xxxxxxxxxxxxxxxxxxxx";
log.LicenseID = "asdfasdfasf";
log.LogConent = "asfdasdfasdfasdfasdf";
log.RegisterNo = "xxxxxxxxxxxxxxxxx";
log.RegisterTime = DateTime.Now;
//修改对象
bool ok = new DbDataUpdate(db).UpdateObject(log, new string[] { "isid" }, new string[] { "isid" });
ShowResult($"操作{ok}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.UpdateList<>()方法,批量更新对象数据:
C# Code:
//
//测试案例:修改对象列表,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user = new sys_AdminUser
{
Account = "000011",
AccountName = "test11",
AccountType = "xx111111111111111111",
Password = "pwd11111111"
};
sys_AdminUser user2 = new sys_AdminUser
{
Account = "000022",
AccountName = "test22222222222",
AccountType = "xx222222222222222222",
Password = "pwd222222222"
};
List<sys_AdminUser> list = new List<sys_AdminUser>();
list.Add(user);
list.Add(user2);
//修改对象列表
int i = new DbDataUpdate(db).UpdateList<sys_AdminUser>(list, new string[] { "Account" }, new string[] { "isid" });
ShowResult($"批量更新记录{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:修改对象列表,根据对象ORM自动存储到数据库
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
sys_AdminUser user = new sys_AdminUser
{
Account = "000011",
AccountName = "test11",
AccountType = "xx111111111111111111",
Password = "pwd11111111"
};
sys_AdminUser user2 = new sys_AdminUser
{
Account = "000022",
AccountName = "test22222222222",
AccountType = "xx222222222222222222",
Password = "pwd222222222"
};
List<sys_AdminUser> list = new List<sys_AdminUser>();
list.Add(user);
list.Add(user2);
//修改对象列表
int i = new DbDataUpdate(db).UpdateList<sys_AdminUser>(list, new string[] { "Account" }, new string[] { "isid" });
ShowResult($"批量更新记录{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.DeleteObject()方法,删除一个对象数据:
C# Code:
//
//测试案例:删除一个对象,根据对象ORM自动自动删除记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
var isid = db.ExecuteScalar("SELECT TOP 1 ISID FROM sys_RegisterLog");
sys_RegisterLog log = new sys_RegisterLog();
log.isid = int.Parse(isid.ToString());
//删除一个对象
bool ok = new DbDataUpdate(db).DeleteObject(log, new string[] { "isid" });
ShowResult($"操作{ok}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:删除一个对象,根据对象ORM自动自动删除记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
var isid = db.ExecuteScalar("SELECT TOP 1 ISID FROM sys_RegisterLog");
sys_RegisterLog log = new sys_RegisterLog();
log.isid = int.Parse(isid.ToString());
//删除一个对象
bool ok = new DbDataUpdate(db).DeleteObject(log, new string[] { "isid" });
ShowResult($"操作{ok}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
调用 DbDataUpdate.DeleteList<>()方法,批量删除对象数据:
C# Code:
//
//测试案例:删除一个对象列表,根据对象ORM自动自动删除记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
var isid = db.GetStringList("SELECT TOP 2 ISID FROM sys_RegisterLog");
sys_RegisterLog R1 = new sys_RegisterLog
{
isid = int.Parse(isid[0]),
};
sys_RegisterLog R2 = new sys_RegisterLog
{
isid = int.Parse(isid[1]),
};
List<sys_RegisterLog> list = new List<sys_RegisterLog>();
list.Add(R1);
list.Add(R2);
//删除一个对象列表
int i = new DbDataUpdate(db).DeleteList<sys_RegisterLog>(list, new string[] { "isid" });
ShowResult($"批量更新记录{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//测试案例:删除一个对象列表,根据对象ORM自动自动删除记录
//
string conn = "Data Source=127.0.0.1;Initial Catalog=CSFramework_License;User ID=sa;Password =test;Persist Security Info=True;Connect Timeout=15;";
IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, conn);
var isid = db.GetStringList("SELECT TOP 2 ISID FROM sys_RegisterLog");
sys_RegisterLog R1 = new sys_RegisterLog
{
isid = int.Parse(isid[0]),
};
sys_RegisterLog R2 = new sys_RegisterLog
{
isid = int.Parse(isid[1]),
};
List<sys_RegisterLog> list = new List<sys_RegisterLog>();
list.Add(R1);
list.Add(R2);
//删除一个对象列表
int i = new DbDataUpdate(db).DeleteList<sys_RegisterLog>(list, new string[] { "isid" });
ShowResult($"批量更新记录{i}");
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网