示例#1
0
        private MySqlCommand GetInsertCommand(t_HK_PGData data)
        {
            MySqlCommand cmd = new MySqlCommand();
            string       InsertSql;
            //反射数据源类
            Type t = data.GetType();

            //取得数据源所有属性
            var pis = t.GetProperties().Where(p => p.Name != "IsSync").ToList();

            var sql      = @"INSERT INTO t_HK_PGData (";
            var valueSql = @" VALUE (";

            //遍历数据源所有属性
            pis.ForEach(p =>
            {
                var value = p.GetValue(data);
                cmd.Parameters.Add(new MySqlParameter("@" + p.Name, value));

                if (pis.Last() != p)
                {
                    sql      += p.Name + ",";
                    valueSql += "@" + p.Name + ",";
                }
                else
                {
                    sql      += p.Name + ")";
                    valueSql += "@" + p.Name + ")";
                }
            });

            InsertSql       = sql + valueSql;
            cmd.CommandText = InsertSql;
            return(cmd);
        }
示例#2
0
        public int Insert(t_HK_PGData data)
        {
            using (MySqlConnection conn = new MySqlConnection(conStr))
            {
                string message = "插入缸号:" + data.batch_no + " 记录\r\n";
                int    result  = 0;
                try
                {
                    MySqlCommand cmd = GetInsertCommand(data);
                    message += "执行SQL语句:" + cmd.CommandText + "\r\n";
                    conn.Open();
                    cmd.Connection = conn;
                    result         = cmd.ExecuteNonQuery();
                    message       += message + "结果:成功";
                    LogHandler.Log(message);
                }
                catch (Exception ex)
                {
                    message += message + "结果:失败\r\n" + "原因:" + ex.Message + "\r\n";
                    LogHandler.Error(message);
                }

                return(result);
            }
        }