c# 获取更新数据库信息
2014-05-07 10:01
309 查看
通过ObjectHelper类对数据库信息进行拷贝和赋值
//新增
public static int CreateUser(JsonUser user){
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
User u = context.User.Create();
ObjectHelper.CopyToObject<User>(user,ref u);
c= context.User.Add(c);
context.SavaChanges();
return c.Id;
}
}
//查询
public static JsonUser RetrieveUser(string accountName, string password, int role)
{
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
var user = (from u in context.User
where u.EmployeeNo == accountName && u.Role == role
select u).FirstOrDefault();
if (user == null)
return null;
else
{
JsonUser ju = new JsonUser();
ObjectHelper.CopyToObject<JsonUser>(user, ref ju);
return ju;
}
}
}
/// <summary>
/// 修改用户
/// </summary>
/// <returns></returns>
public static bool UpdateUsers(JsonUser user)
{
bool result = false;
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
User c = context.User.Find(user.Id);
if (c != null)
{
ObjectHelper.CopyToObject<User>(user, ref c);
result = (context.SaveChanges() == 1);
}
}
return result;
}
---------------------------------------------------------------------------------------------------------
class ObjectHelper
{
/// <summary>
/// Copy an object to destination object, only matching fields will be copied
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sourceObject">An object with matching fields of the destination object</param>
/// <param name="destObject">Destination object, must already be created</param>
public static void CopyToObject<T>(object sourceObject, ref T destObject)
{
// If either the source, or destination is null, return
if (sourceObject == null || destObject == null)
return;
// Get the type of each object
Type sourceType = sourceObject.GetType();
Type targetType = destObject.GetType();
// Loop through the source properties
foreach (PropertyInfo p in sourceType.GetProperties())
{
// Get the matching property in the destination object
PropertyInfo targetObj = targetType.GetProperty(p.Name);
// If there is none, skip
if (targetObj == null)
continue;
// Set the value in the destination
targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
}
return;
}
}
}
}
//新增
public static int CreateUser(JsonUser user){
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
User u = context.User.Create();
ObjectHelper.CopyToObject<User>(user,ref u);
c= context.User.Add(c);
context.SavaChanges();
return c.Id;
}
}
//查询
public static JsonUser RetrieveUser(string accountName, string password, int role)
{
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
var user = (from u in context.User
where u.EmployeeNo == accountName && u.Role == role
select u).FirstOrDefault();
if (user == null)
return null;
else
{
JsonUser ju = new JsonUser();
ObjectHelper.CopyToObject<JsonUser>(user, ref ju);
return ju;
}
}
}
/// <summary>
/// 修改用户
/// </summary>
/// <returns></returns>
public static bool UpdateUsers(JsonUser user)
{
bool result = false;
using (TeleMedicineEntities context = new TeleMedicineEntities())
{
User c = context.User.Find(user.Id);
if (c != null)
{
ObjectHelper.CopyToObject<User>(user, ref c);
result = (context.SaveChanges() == 1);
}
}
return result;
}
---------------------------------------------------------------------------------------------------------
class ObjectHelper
{
/// <summary>
/// Copy an object to destination object, only matching fields will be copied
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sourceObject">An object with matching fields of the destination object</param>
/// <param name="destObject">Destination object, must already be created</param>
public static void CopyToObject<T>(object sourceObject, ref T destObject)
{
// If either the source, or destination is null, return
if (sourceObject == null || destObject == null)
return;
// Get the type of each object
Type sourceType = sourceObject.GetType();
Type targetType = destObject.GetType();
// Loop through the source properties
foreach (PropertyInfo p in sourceType.GetProperties())
{
// Get the matching property in the destination object
PropertyInfo targetObj = targetType.GetProperty(p.Name);
// If there is none, skip
if (targetObj == null)
continue;
// Set the value in the destination
targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
}
return;
}
}
}
}
相关文章推荐
- GET请求12306网站链接获取火车站代号信息更新到后台数据库表中
- C#如何重DataSet的DataTable里面删除信息并更新到数据库
- C# 获取数据库表信息,列信息
- 针对不同数据库,获取当前用户所有有权限查看的表,以及表的创建时间、更新时间、注释等信息,表中字段的相关信息(包含分页实现)
- GET请求12306网站链接获取火车站代号信息更新到后台数据库表中
- 获取SVN版本信息更新exe,dll文件的版本信息(C++, C#)
- C# 获取数据库表信息与列信息的方法总结
- C#中如何获取数据库中表的信息和列的信息
- C# 更新SQL Server数据库备注信息从另一数据库
- C#获取数据库表信息与列信息的方法
- mysql+C#实战二:获取数据库构架信息
- C#中如何获取数据库中表的信息和列的信息
- 【C#】C#实现更新数据库信息
- 本文使用c#通过oledb可获取excel文件表的结构信息,同理也可以获得access等数据库的表结构信息,实现如下:
- C#中如何获取数据库中表的信息和列的信息
- 黄聪:destoon常用数据库操作(插入、更新、删除、获取一行信息)
- C#中如何获取数据库中表的信息和列的信息
- C#中往数据库插入/更新时候关于NUll空值的处理
- c#实现Google账号登入授权(OAuth 2.0)并获取个人信息
- C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息