又开始搬砖了,今天主要是记录一下年前到现在的笔记。
C#语言,visual studio工具。
写一个保存的函数:
思路1:先删除原有的数据再新增;
思路2:直接更新update。
先上整个函数代码:
public void Save(HttpContext context)
{
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
var jss = new JavaScriptSerializer();
try
{
List<Command> commands = new List<Command>();
BrcodeParser entity = jss.Deserialize<BrcodeParser>(stream);
if (entity.PARS_NO != null && entity.PARS_NO != "")
{
//查找表MTL_PARSE_ITEM有无对应数据,"where PARS_NO='{0}' and ITEM_NO='{1}'"是条件,entity.PARS_NO, entity.ITEM_NO是对应属性的值
//方法GetList是查找多个,即返回的类型是list。查找单个的是使用Locate
List<MTL_PARSE_ITEM> _items = MTL_PARSE_ITEM.GetList(string.Format("where PARS_NO='{0}' and ITEM_NO='{1}'", entity.PARS_NO, entity.ITEM_NO));
if (_items.Count == 0)
return;
//有对应数据则把前端穿过来的值赋给对应字段
_items[0].SEPARATOR = entity.SEPARATOR;
_items[0].SEG_NUM = entity.ARRAY_LEN.ToString();
_items[0].COMMNET = entity.PARS_DESC;
//直接更新为_item[0]的值
commands.Add(_items[0].GetCommand(Transaction.TextType.Update));
MTL_PARSE_INFO head = MTL_PARSE_INFO.Locate(entity.PARS_NO);
head.PARS_DESC = entity.PARS_DESC;
head.SEPARATOR = entity.SEPARATOR;
head.ARRAY_LEN = entity.ARRAY_LEN;
head.UPDATE_DATE = DateTime.Now;
head.UPDATE_USER = context.User.Identity.Name;
commands.Add(head.GetCommand(Transaction.TextType.Update));
//以下是思路1 先删后增
List<MTL_PARSE_LINE> lines = entity.Lines;
int i = 1;
foreach (MTL_PARSE_LINE line in lines)
{
List<MTL_PARSE_LINE> _line = MTL_PARSE_LINE.GetList(string.Format("where PARS_NO='{0}' and SEQNR='{1}'", head.PARS_NO, line.SEQNR));
if(_line.Count!=0){
commands.Add(line.GetCommand(Transaction.TextType.Delete));
}
line.SEQNR = i;//这里的SEQNR字段是我表里的字段,是不能重复且要从1开始递增的
commands.Add(line.GetCommand(Transaction.TextType.Insert));
i++;
}
}
KQuery query = new KQuery();
query.Execute(commands);
m_responseData = new ResponseData(true, "保存成功.");//返回的提示信息
}
catch (Exception ex)
{
m_responseData = new ResponseData(false, ex.Message);
}
string jsonString = JsonConvert.SerializeObject(m_responseData);
context.Response.Write(jsonString);
}