Previous topicNext topic
Help > 开发指南 > SanMuGrid平台编程 > 主要对象 > Database > ADO数据处理 >
在内存表中增删改查

我们在需要直接对数据库中的表进行增、删、改、查操作时,大多数时候是不需要将数据加载到窗体表中的,只需要将表数据加载到内存中进行操作。 而操作的方式可以通过DataTableMap和DataTableHelp两种对象进行操作。在使用DataTableMap对象对数据库表进行操作时,必须先生成相应的数据对象类才可以。

 VB.Net
 
Dim db As Database=Proj.SysDataFactory("UserDB")

'示例1,用ORM技术修改一条记录
Dim map As DataTableMap(Of EmployeeInfo)=db.ExecuteDataTableMap(Of EmployeeInfo)(True)
map.LoadFilter="EmployeeID='U000000003'"
map.Load
If map IsNot Nothing AndAlso map.DataObjects.Count>0 Then
    '单记录处理
    Dim emp As EmployeeInfo=map.DataObjects(0)
    emp.AddressArea="太仓"
    emp.AddressCity="苏州"
    emp.AddressProvince="江苏"
    emp.Save  '单对象保存
    
    '遍历处理
    For Each dr As EmployeeInfo In map.DataObjects
        dr.AddressArea="太仓"
        dr.AddressCity="苏州"
        dr.AddressProvince="江苏"
        'dr.Save  '也可以针对单对象保存
    Next
    '整个表保存
    map.Save
End If

'示例2,用普通方法修改一条记录
Dim dt As DataTableHelp= db.ExecuteDataTableHelp("select * from EmployeeInfo where EmployeeID='U000000003'",True)
If dt IsNot Nothing AndAlso dt.DataRows.Count>0 Then
    '单记录处理
    Dim dr As RowData=dt.DataRows(0)
    dr("AddressArea")="太仓"
    dr("AddressCity")="苏州"
    dr("AddressProvince")="江苏"
    dr.Save
    
    '遍历处理
    For Each item As RowData In dt.DataRows
        item("AddressArea")="太仓"
        item("AddressCity")="苏州"
        item("AddressProvince")="江苏"
        'item.Save  '也可以针对单行保存
    Next
    '整个表保存
    dt.Save
End If

 C#
Database db = Proj.SysDataFactory["UserDB"];

// 示例1,用ORM技术修改一条记录
DataTableMap<EmployeeInfo> map = db.ExecuteDataTableMap<EmployeeInfo>(true);
map.LoadFilter = "EmployeeID='U000000003'";
map.Load();
if (map != null && map.DataObjects.Count > 0)
{
    // 单记录处理
    EmployeeInfo emp = map.DataObjects[0];
    emp.AddressArea = "太仓";
    emp.AddressCity = "苏州";
    emp.AddressProvince = "江苏";
    emp.Save();  // 单对象保存
    
    // 遍历处理
    foreach (EmployeeInfo dr in map.DataObjects)
    {
        dr.AddressArea = "太仓";
        dr.AddressCity = "苏州";
        dr.AddressProvince = "江苏";
    }
    // 整个表保存
    map.Save();
}

// 示例2,用普通方法修改一条记录
DataTableHelp dt = db.ExecuteDataTableHelp("select * from EmployeeInfo where EmployeeID='U000000003'", true);
if (dt != null && dt.DataRows.Count > 0)
{
    // 单记录处理
    RowData dr = dt.DataRows[0];
    dr["AddressArea"] = "太仓";
    dr["AddressCity"] = "苏州";
    dr["AddressProvince"] = "江苏";
    dr.Save();
    
    // 遍历处理
    foreach (RowData item in dt.DataRows)
    {
        item["AddressArea"] = "太仓";
        item["AddressCity"] = "苏州";
        item["AddressProvince"] = "江苏";
    }
    // 整个表保存
    dt.Save();
}

Vb.Net
Dim db As Database=Proj.SysDataFactory("UserDB")

'示例1,用ORM技术新增一条记录
Dim map As DataTableMap(Of EmployeeInfo)=db.ExecuteDataTableMap(Of EmployeeInfo)(True)
'新增一条数据
Dim emp As EmployeeInfo=map.AddNewObject()
emp.AddressArea="太仓"
emp.AddressCity="苏州"
emp.AddressProvince="江苏"
emp.Save  '单对象保存


'示例2,用普通方法新增一条记录
Dim dt As DataTableHelp= db.ExecuteDataTableHelp("select * from EmployeeInfo where 1=2",True)
Dim dr As RowData=dt.AddNew()
dr("AddressArea")="太仓"
dr("AddressCity")="苏州"
dr("AddressProvince")="江苏"
dr.Save

C#
Database db = Proj.SysDataFactory["UserDB"];

// 示例1,用ORM技术新增一条记录
DataTableMap<EmployeeInfo> map = db.ExecuteDataTableMap<EmployeeInfo>(true);
// 新增一条数据
EmployeeInfo emp = map.AddNewObject();
emp.AddressArea = "太仓";
emp.AddressCity = "苏州";
emp.AddressProvince = "江苏";
emp.Save();  // 单对象保存


// 示例2,用普通方法新增一条记录
DataTableHelp dt = db.ExecuteDataTableHelp("select * from EmployeeInfo where 1=2", true);
RowData dr = dt.AddNew();
dr["AddressArea"] = "太仓";
dr["AddressCity"] = "苏州";
dr["AddressProvince"] = "江苏";
dr.Save();

 VB.Net
Dim db As Database=Proj.SysDataFactory("UserDB")

'示例1,用ORM技术删除一条记录
Dim map As DataTableMap(Of EmployeeInfo)=db.ExecuteDataTableMap(Of EmployeeInfo)(True)
map.LoadFilter="EmployeeID='U000000003'"
map.Load
If map IsNot Nothing AndAlso map.DataObjects.Count>0 Then
    '单记录处理
    Dim emp As EmployeeInfo=map.DataObjects(0)
    emp.Delete
    map.Save  '单对象保存
End If



'示例2,用普通方法删除一条记录
Dim dt As DataTableHelp= db.ExecuteDataTableHelp("select * from EmployeeInfo where EmployeeID='U000000003'",True)
If dt IsNot Nothing AndAlso dt.DataRows.Count>0 Then
    '删除
    Dim dr As RowData=dt.DataRows(0)
    dr.Delete
    dt.Save
End If

C#
Database db = Proj.SysDataFactory["UserDB"];

// 示例1,用ORM技术删除一条记录
DataTableMap<EmployeeInfo> map = db.ExecuteDataTableMap<EmployeeInfo>(true);
map.LoadFilter = "EmployeeID='U000000003'";
map.Load();
if (map != null && map.DataObjects.Count > 0)
{
    // 单记录处理
    EmployeeInfo emp = map.DataObjects[0];
    emp.Delete();
    map.Save();  // 单对象保存
}

// 示例2,用普通方法删除一条记录
DataTableHelp dt = db.ExecuteDataTableHelp("select * from EmployeeInfo where EmployeeID='U000000003'", true);
if (dt != null && dt.DataRows.Count > 0)
{
    // 删除
    RowData dr = dt.DataRows[0];
    dr.Delete();
    dt.Save();
}