C#求指导答这个函数要怎么写

.Net技术 码拜 5年前 (2016-05-18) 799次浏览
 public class SysServerData : ActionServiceBase
    {
        //服务器用户Id
        public static int uid = 0;
        public int _pagesize = 50;
        public string currentTimeStamp = "0";
        private SysDataRecordService sysDataRecordService = new SysDataRecordService();
        private EventService eventService = new EventService();
        private SessionService sessionService = new SessionService();
        private TOrderService orderService = new TOrderService();
        private List<Event> eventList = null;
        private List<Session> sessionList = null;
        //同步活动列表
        public void SysEventList(string org_id)
        {
            try
            {
                //string timeStamp = sysDataRecordService.getTimeStampByTableName(uid,"Event", new Guid());
                Paging<EventUrl> eUrlPage = UrlFunc.EventList();
                if (eUrlPage == null || eUrlPage.List == null || eUrlPage.List.Count <= 0) return;
                BeginTransaction();
                eventList = eventService.GetList().List.ToList();
                if (eUrlPage.List != null && eUrlPage.List.Count > 0)
                {
                    UIdRelateEventService ueService = new UIdRelateEventService();
                    foreach (EventUrl item in eUrlPage.List)
                    {
                        Event tempEvent = eventList.FirstOrDefault(d => d.Event_Id == item.event_id);
                        Guid tempId = new Guid();
                        if (tempEvent != null)
                        {
                            tempId = tempEvent.Id;
                            Event eObj = tempEvent;
                            eObj.SponsorName = "";
                            eObj.Title = item.title;
                            eObj.SubTitle = "";//SysDataRecordService.subStringByLength(item.sub_title, 500);
                            eObj.ActivityIntroduce = SysDataRecordService.subStringByLength(item.introduce, 5000);
                            eObj.Site = SysDataRecordService.subStringByLength(item.place_name, 200);
                            eObj.Province = item.province;
                            eObj.City = item.city;
                            eObj.StartDate = item.sdate;
                            eObj.EndDate = item.edate;
                            eObj.Status = item.status;
                            eObj.Address = SysDataRecordService.subStringByLength(item.address, 500);
                            eObj.Code = item.event_id.ToString();
                            eObj.Event_Class = item.class_name;
                            eventService.Update(eObj);
                        }
                        else {
                            Event eObj = new Event();
                            eObj.Id = Guid.NewGuid();
                            tempId = eObj.Id;
                            eObj.Event_Id = item.event_id;
                            eObj.SponsorName = "";
                            eObj.Title = item.title;
                            eObj.SubTitle = SysDataRecordService.subStringByLength(item.sub_title, 500);
                            eObj.ActivityIntroduce = SysDataRecordService.subStringByLength(item.introduce, 5000);
                            eObj.Site = SysDataRecordService.subStringByLength(item.place_name, 200);
                            eObj.Province = item.province;
                            eObj.City = item.city;
                            eObj.StartDate = item.sdate;
                            eObj.EndDate = item.edate;
                            eObj.Status = item.status;
                            eObj.Address = SysDataRecordService.subStringByLength(item.address, 500);
                            eObj.Code = item.event_id.ToString();
                            eObj.Event_Class = item.class_name;
                            eventService.Insert(eObj);
                        }

这个Update和Insert函数要怎么写

   public void Update(Event obj)
        {
           
        }
        public void Insert(Event obj)
        {
            
        }
解决方案

10

就是修改、插入的sql语句

insert into table(field1,field2,....)values(value1,value2,...)
--前面field1,field2...,需要知道数据库表的结构,字段名,后面value1,value2,...由参数eObj传入

10

当然你可以用参数法,而不是本人这样直接插入,只是举例
另外,这个插入、更新的方法,是要写入对象eventService的类型里,也就是EventService类里

20

你就不要搞什么list了,像下面这样

public void Insert(Event obj)
{
    try
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString2"].ToString());
        con.Open();
        string sql = "insert into table(field1,field2)values(@value1,@value2)";
        SqlCommand com = new SqlCommand(sql, con);
        com.Parameters.Add(new SqlParameter("@value1", SqlDbType.NChar)).Value = obj.Event_Id;
        com.Parameters.Add(new SqlParameter("@value2", SqlDbType.NChar)).Value = obj.SponsorName;
        if (com.ExecuteNonQuery() > 0)
        {
            //提示成功
        }
        else
        {
            //提示失败
        }
        com.Dispose();
        con.Close();
    }
    catch
    {
        //...........
    }
}

field1,field2是数据库里,存放obj.Event_Id,obj.SponsorName的字段名
eObj有很多字段,其他的如法炮制
eObj.Event_Id = item.event_id;
eObj.SponsorName = “”;
eObj.Title = item.title;
eObj.SubTitle = SysDataRecordService.subStringByLength(item.sub_title, 500);
eObj.ActivityIntroduce = SysDataRecordService.subStringByLength(item.introduce, 5000);
eObj.Site = SysDataRecordService.subStringByLength(item.place_name, 200);
eObj.Province = item.province;
eObj.City = item.city;
eObj.StartDate = item.sdate;
eObj.EndDate = item.edate;
eObj.Status = item.status;
eObj.Address = SysDataRecordService.subStringByLength(item.address, 500);
eObj.Code = item.event_id.ToString();
eObj.Event_Class = item.class_name;
eventService.Insert(eObj);


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#求指导答这个函数要怎么写
喜欢 (0)
[1034331897@qq.com]
分享 (0)