Ejemplo n.º 1
0
        //将数据保存到数据库
        public bool SaveData(List <Model> models, int xmno)
        {
            DataProcessCommon common = new DataProcessCommon();
            OdbcConnection    conn   = DbContext.GetConn(xmno);

            conn.Open();
            OdbcTransaction trans = conn.BeginTransaction();

            try
            {
                foreach (var item in models)
                {
                    //获取点名
                    item.pointName = common.GetPointName(item.sno, item.tdno);
                    string sql = string.Format("insert into data(sno, cycle, time, tdno, res, pointName, dy, port, dataType) values('{0}', '{1}', '{2}', '{3}', {4}, '{5}', {6}, {7}, '{8}')",
                                               item.sno, item.cycle, item.time, item.tdno, item.res, item.pointName, item.dy, item.port, item.dataType);
                    OdbcCommand comm = new OdbcCommand(sql, conn);
                    comm.Transaction = trans;
                    if (comm.ExecuteNonQuery() <= 0)
                    {
                        trans.Rollback();
                        return(false);
                    }
                }
                trans.Commit();
                return(true);
            }
            catch (Exception ex)
            {
                string info = string.Format("数据保存的数据库失败, {0}", ex.Message);
                FileOperation.WriteAppenFile(info);
                trans.Rollback();
                return(false);
            }
            finally
            {
                trans.Dispose();
                conn.Close();
            }
        }