项目收集-AutoMapper使用,事务,Json.Net序列化反序列化,代码生成调用等

时间:2024-04-20 16:36:21
using System;
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; namespace ConsoleApplication1
{
class Program
{ private const string ConnStr = "Data Source=192.168.1.88;User Id=sa;Password=1234567890;Database=dbtest;Pooling=true;Max Pool Size=600;Min Pool Size=0;";
static void Main(string[] args)
{
//automapper
productDto dto = new productDto();
dto.proId = 1000;
dto.proName = "2016款最新秋季男式外套"; //AddressDto到Address的映射,AddressDto的字段CountryName要对应Address的字段Country:
//Mapper.CreateMap<AddressDto, Address>().ForMember(d => d.Country, opt => opt.MapFrom(s => s.CountryName)); Mapper.Initialize(x => x.CreateMap<productDto, product>());
var model = Mapper.Map<product>(dto);
Console.WriteLine(string.Format("{0}-{1}-{2}-{3}-{4}", model.proId, model.Price, model.shortInfo, model.info, model.addDate));
Console.ReadKey(); JObject obj = new JObject();
obj.Add("proId", "20000");
obj.Add("proName", "品牌电脑");
obj.Add("Price", "3600.00");
//转换成json字符串
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
Console.ReadKey();
//json字符串转换成对象 方式1
var jsonObj = JObject.Parse(json); //JsonConvert.DeserializeObject(json);
Console.WriteLine(string.Format("{0}-{1}-{2}", jsonObj["proId"], jsonObj["proName"], jsonObj["Price"]));
Console.ReadKey();
//json字符串转换成对象 方式2
var jsonModel = JsonConvert.DeserializeObject(json);
Console.WriteLine(string.Format("{0}-{1}-{2}", jsonObj["proId"], jsonObj["proName"], jsonObj["Price"]));
Console.ReadKey();
//json字符串转换成对象 方式3
var proDto = JsonConvert.DeserializeObject<productDto>(json);
Console.WriteLine(string.Format("{0}-{1}-{2}", proDto.proId, proDto.proName, proDto.Price));
Console.ReadKey();
/*
Maticsoft.DBUtility.DbHelperSQL.connectionString = ConnStr; GetAllData();
Console.ReadKey(); AddData();
Console.ReadKey();
GetAllData();
Console.ReadKey(); UpdateData();
Console.ReadKey();
GetAllData();
Console.ReadKey(); GetQueryData();
Console.ReadKey(); DeleteData();
Console.ReadKey(); GetQueryData();
Console.ReadKey(); DeleteData2();
Console.ReadKey(); GetQueryData();
Console.ReadKey(); //事务处理
using (TransactionScope scope = new TransactionScope())
{
scope.Complete();
}
*/
} private static void GetAllData()
{
Console.WriteLine("===============全部数据=================");
//获取全部数据
foreach (var item in Dec.BLL.GatherData.GetAllGatherDatas())
{
Console.WriteLine(string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}", item.Url, item.SkuCode, item.Domain,
item.Lang, item.Currency, item.ProName, item.ProGroup));
}
} private static void GetQueryData()
{
Console.WriteLine("===============查询数据=================");
//获取全部数据(搜索条件)
Dec.Models.GatherData queryModel = new Dec.Models.GatherData();
queryModel.Url = "22";
queryModel.Domain = "44";
foreach (var item in Dec.BLL.GatherData.GetAllGatherDatasByQuery(queryModel))
{
Console.WriteLine(string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}", item.Url, item.SkuCode, item.Domain,
item.Lang, item.Currency, item.ProName, item.ProGroup));
}
} private static void AddData()
{
Console.WriteLine("===============新增数据=================");
//新增数据
Dec.Models.GatherData addModel = new Dec.Models.GatherData();
addModel.Url = "22";
addModel.SkuCode = "33";
addModel.Domain = "44"; addModel.Lang = "55";
addModel.Currency = "66";
addModel.ProName = "77";
addModel.ProGroup = "88";
int i = Dec.BLL.GatherData.Add(addModel);
if (i > 0)
{
Console.WriteLine("插入成功");
}
else
{
Console.WriteLine("插入失败");
}
} private static void UpdateData()
{
Console.WriteLine("===============更新数据=================");
//更新数据
Dec.Models.GatherData updateModel = new Dec.Models.GatherData();
updateModel.Id = 10000;
updateModel.Url = "2222";
updateModel.SkuCode = "3333";
updateModel.Domain = "4444"; updateModel.Lang = "5555";
updateModel.Currency = "6666";
updateModel.ProName = "7777";
updateModel.ProGroup = "8888";
bool flag = Dec.BLL.GatherData.Update(updateModel);
if (flag)
{
Console.WriteLine("更新成功");
}
else
{
Console.WriteLine("更新失败");
}
} private static void DeleteData()
{
Console.WriteLine("===============删除数据=================");
bool flag = Dec.BLL.GatherData.Delete(10000);
if (flag)
{
Console.WriteLine("删除成功");
}
else
{
Console.WriteLine("删除失败");
}
} private static void DeleteData2()
{
Console.WriteLine("===============删除数据=================");
//更新数据
Dec.Models.GatherData updateModel = new Dec.Models.GatherData();
updateModel.Url = "22";
updateModel.SkuCode = "33";
bool flag = Dec.BLL.GatherData.Delete(updateModel);
if (flag)
{
Console.WriteLine("删除成功");
}
else
{
Console.WriteLine("删除失败");
}
}
} public class productDto
{
public int proId { set; get; }
public string proName { set; get; }
public double Price { set; get; }
} public class product
{
public int proId { set; get; }
public string proName { set; get; }
public double Price { set; get; }
public string shortInfo { set; get; }
public string info { set; get; }
public DateTime addDate { set; get; }
}
}

项目收集-AutoMapper使用,事务,Json.Net序列化反序列化,代码生成调用等