Ejemplo n.º 1
0
        /// <summary>
        /// Create a .cs file from a SQL entity
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="_t"></param>
        /// <param name="_path"></param>
        /// <param name="_nameSpace"></param>
        private static void CreatePocoEntity(SqlConnection connection, string _t, string _path, string _nameSpace, bool _isTable = true)
        {
            using (var classText = new Classer(_nameSpace, _t))
            {
                string _sqlCmd;

                if (_isTable)
                {
                    _sqlCmd = Constants.QUERY_FOR_TABLE_FIELDS;
                }
                else
                {
                    classText.Append("\t\tpublic void " + _t + "(");

                    _sqlCmd = Constants.QUERY_FOR_PROCEDURE_FIELDS;
                }

                var _cmd = new SqlCommand(_sqlCmd, connection);
                _cmd.Parameters.Add(new SqlParameter("objName", _t));

                var _da = new SqlDataAdapter(_cmd);
                var _dt = new DataTable();
                _da.Fill(_dt);

                var _numRows = 1;
                foreach (DataRow r in _dt.Rows)
                {
                    var _type = GetNETType(r.Field <string>(1));
                    var _name = r.Field <string>(2);

                    if (_isTable)
                    {
                        if (r.Field <bool>(4))
                        {
                            classText.AddDataAnnotation(Constants.EF_KEY_DA);
                            classText.AddDataAnnotation(Constants.EF_COLUMN_ORDER(r.Field <int>(5) - 1));
                        }

                        if (r.Field <bool>(6))
                        {
                            classText.AddDataAnnotation(Constants.EF_IDENTITY_DA);
                        }

                        if (r.Field <bool>(7))
                        {
                            classText.AddDataAnnotation(Constants.EF_COMPUTED_DA);
                        }

                        classText.AddPublicProperty(_type + (r.Field <bool>(3) ? "?" : ""), _name);
                    }
                    else
                    {
                        classText.AddArgument(_type, _name, _numRows++ < _dt.Rows.Count);
                    }
                }

                if (!_isTable)
                {
                    classText.AppendLine(")")
                    .AppendLine("\t\t{")
                    .AppendLine("\t\t\t// TO DO: Implement function calling")
                    .AppendLine("\t\t}");
                }

                classText.Close();

                if (_isTable)
                {
                    File.WriteAllText(Path.Combine(_path, _t + ".cs"), classText.ToString());
                }
            }
        }