Пример #1
0
        private static string GerarSolectNode2(SqlTable sqlTable)
        {
            StringBuilder builder  = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            SqlTable      table    = SqlTable.GetTable(sqlTable.References[0].PkTable);
            SqlTable      table2   = SqlTable.GetTable(sqlTable.References[1].PkTable);

            builder2.Append(table2.GetIdentityColumn().NetDataType + " " + table2.GetIdentityColumn().Name + "_, ");
            builder.AppendLine(string.Concat(new object[] { "\t\t/// <summary>\n\t\t/// Busca os " + table.ClassBusinessNome + " na base atrav\x00e9s do par\x00e2metro\n\t\t/// </summary>\n\t\tpublic DbDataReader Get_", table.ClassBusinessNome, "(", builder2.Remove(builder2.Length - 2, 2), ")" }));
            builder.AppendLine("\t\t{");
            builder.AppendLine("\t\t\ttry");
            builder.AppendLine("\t\t\t{");
            builder.AppendLine("\t\t\t\tcmd = new SqlCommand();");
            builder.AppendLine("\t\t\t\tcmd.CommandType = CommandType.StoredProcedure;");
            string str = "";

            str += "\t\t\t\tcmd.CommandText = \"usp_";
            str += Program.sulfixSelect;
            str += table.ClassBusinessNome;
            str += "By";
            str += table2.GetIdentityColumn().Name[0].ToString().ToUpper() + table2.GetIdentityColumn().Name.Substring(1);
            str += "\";";
            builder.AppendLine(str);
            builder.AppendLine("\t\t\t\tcmd.Parameters.AddWithValue(\"@" + table2.GetIdentityColumn().Name + "\", " + table2.GetIdentityColumn().Name + "_);");
            builder.AppendLine();
            builder.AppendLine(Tab(4) + "OpenConnection();");
            builder.AppendLine("\t\t\t\treturn cmd.ExecuteReader(CommandBehavior.CloseConnection);");
            builder.AppendLine("\t\t\t}");
            builder.AppendLine("\t\t\tcatch (SqlException sex){ throw sex; }");
            builder.AppendLine("\t\t\tcatch (Exception ex){ throw ex; }");
            builder.AppendLine("\t\t}");
            builder.AppendLine();
            builder.AppendLine();
            StringBuilder builder3 = new StringBuilder();

            builder3.Append(table.GetIdentityColumn().NetDataType + " " + table.GetIdentityColumn().Name + "_, ");
            builder.AppendLine(string.Concat(new object[] { "\t\t/// <summary>\n\t\t/// Busca os " + table2.ClassBusinessNome + " na base atrav\x00e9s do par\x00e2metro\n\t\t/// </summary>\n\t\tpublic DbDataReader Get_", table2.ClassBusinessNome, "(", builder3.Remove(builder3.Length - 2, 2), ")" }));
            builder.AppendLine("\t\t{");
            builder.AppendLine("\t\t\ttry");
            builder.AppendLine("\t\t\t{");
            builder.AppendLine("\t\t\t\tcmd = new SqlCommand();");
            builder.AppendLine("\t\t\t\tcmd.CommandType = CommandType.StoredProcedure;");
            builder.AppendLine("\t\t\t\tcmd.CommandText = \"usp_" + Program.sulfixSelect + table2.ClassBusinessNome + "By" + table.GetIdentityColumn().Name[0].ToString().ToUpper() + table.GetIdentityColumn().Name.Substring(1) + "\";");
            builder.AppendLine("\t\t\t\tcmd.Parameters.AddWithValue(\"@" + table.GetIdentityColumn().Name + "\", " + table.GetIdentityColumn().Name + "_);");
            builder.AppendLine();
            builder.AppendLine(Tab(4) + "OpenConnection();");
            builder.AppendLine("\t\t\t\treturn cmd.ExecuteReader(CommandBehavior.CloseConnection);");
            builder.AppendLine("\t\t\t}");
            builder.AppendLine("\t\t\tcatch (SqlException sex){ throw sex; }");
            builder.AppendLine("\t\t\tcatch (Exception ex){ throw ex; }");
            builder.AppendLine("\t\t}");
            return(builder.ToString());
        }
Пример #2
0
        public static string GerarInsertUpdate(SqlTable sqlTable)
        {
            string format = "";

            if (!sqlTable.IsTableNo)
            {
                format = format + string.Format("-- *****************************************************\n-- *** Criado em ........ {0}\n-- *** Criado por ....... CSGen\n-- *** Descricao ........ Procedure criada para {1} registros na tabela {2}\n-- *** Alterado em ......\n-- *** Alterado por .....\n-- *** Motivo alteracao .\n-- *****************************************************\n\n", string.Format("{0:dd/MM/yyyy HH:mm}", DateTime.Now), "inserir ou atualizar", sqlTable.Nome);
                format = format + string.Format("IF EXISTS (SELECT * FROM sysobjects \nWHERE name = N'{0}'\nAND type = 'P')\nDROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", sqlTable.ProcNome, Program.sulfixInsert)) + string.Format("create proc usp_{1}{0}\n", sqlTable.ProcNome, Program.sulfixInsert) + GerarParametros(sqlTable.Colunas) + "set nocount on\n";
                foreach (SqlColumn column in sqlTable.Colunas)
                {
                    if (column.IsIdentity)
                    {
                        format = format + string.Format("if( {0} )\nbegin\n\t{1}\nend\n", string.Format("@{0} is null or @{0} = 0", column.Name), "{0}");
                    }
                }
                string str2 = "";
                string str3 = "";
                string str4 = "";
                str2 = "insert into " + sqlTable.Nome + " ({0}) \n\tvalues ({1}) \n\tselect SCOPE_IDENTITY() as 'identity'";
                foreach (SqlColumn column2 in sqlTable.Colunas)
                {
                    if (!column2.IsIdentity)
                    {
                        str3 = str3 + "" + column2.Name + ", ";
                        str4 = str4 + "@" + column2.Name + ", ";
                    }
                }
                if (str3 != string.Empty)
                {
                    str3 = str3.Remove(str3.Length - 2);
                }
                if (str4 != string.Empty)
                {
                    str4 = str4.Remove(str4.Length - 2);
                }
                str2   = string.Format(str2, str3, str4);
                format = string.Format(format, string.Format(str2, str2)) + "else \nbegin\n\t{0}\nend";
                string str5 = "update " + sqlTable.Nome + " set ";
                foreach (SqlColumn column3 in sqlTable.Colunas)
                {
                    if (!column3.IsIdentity)
                    {
                        string str6 = str5;
                        str5 = str6 + "" + column3.Name + " = @" + column3.Name + ", ";
                    }
                }
                if (str5 != string.Empty)
                {
                    str5 = str5.Remove(str5.Length - 2) + "\n\tWhere ";
                }
                foreach (SqlColumn column4 in sqlTable.Colunas)
                {
                    if (column4.IsIdentity)
                    {
                        string str7 = str5;
                        str5 = str7 + "" + column4.Name + " = @" + column4.Name + " and";
                    }
                }
                if (str5 != string.Empty)
                {
                    str5 = str5.Remove(str5.Length - 3);
                }
                str5   = str5 + "\n\tselect @" + sqlTable.GetIdentityColumn().Name;
                format = string.Format(format, str5);
            }
            else
            {
                format = format + string.Format("-- *****************************************************\n-- *** Criado em ........ {0}\n-- *** Criado por ....... CSGen\n-- *** Descricao ........ Procedure criada para {1} registros na tabela {2}\n-- *** Alterado em ......\n-- *** Alterado por .....\n-- *** Motivo alteracao .\n-- *****************************************************\n\n", string.Format("{0:dd/MM/yyyy HH:mm}", DateTime.Now), "inserir ou atualizar", sqlTable.Nome);
                format = format + string.Format("IF EXISTS (SELECT * FROM sysobjects \nWHERE name = N'{0}'\nAND type = 'P')\nDROP PROCEDURE {0} \n#go#\n\n", string.Format("usp_{1}{0}", sqlTable.ProcNome, Program.sulfixInsert)) + string.Format("create proc usp_{1}{0}\n", sqlTable.ProcNome, Program.sulfixInsert) + GerarParametros(sqlTable.Colunas) + "set nocount on\n";
                string str8  = "";
                string str9  = "";
                string str10 = "";
                str8 = "insert into " + sqlTable.Nome + " ({0}) \nvalues ({1}) \n";
                foreach (SqlColumn column5 in sqlTable.Colunas)
                {
                    if (!column5.IsIdentity)
                    {
                        str9  = str9 + "" + column5.Name + ", ";
                        str10 = str10 + "@" + column5.Name + ", ";
                    }
                }
                if (str9 != string.Empty)
                {
                    str9 = str9.Remove(str9.Length - 2);
                }
                if (str10 != string.Empty)
                {
                    str10 = str10.Remove(str10.Length - 2);
                }
                str8   = string.Format(str8, str9, str10);
                format = format + str8;
            }
            return(format + "\n\n#go#\n\n");
        }
Пример #3
0
        private static string GerarInsertOrUpdate(SqlTable sqlTable)
        {
            StringBuilder builder  = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            string        str      = "int";

            if (!(sqlTable.IsTableNo || !(sqlTable.GetIdentityColumn().NetDataType == "decimal?")))
            {
                str = "decimal";
            }
            string str2 = "";

            foreach (SqlColumn column in sqlTable.Colunas)
            {
                if (column.IsIdentity)
                {
                    builder2.Append("ref " + column.NetDataType + " " + column.Name + "_, ");
                    str2 = column.Name + "_";
                }
                else
                {
                    builder2.Append(column.NetDataType + " " + column.Name + "_, ");
                }
            }
            builder.AppendLine("\t\t/// <summary>\n\t\t/// Salva os dados na base atrav\x00e9s de par\x00e2metros\n\t\t/// </summary>\n\t\tpublic DbTransaction Save(" + builder2.Remove(builder2.Length - 2, 2) + ")");
            builder.AppendLine("\t\t{");
            builder.AppendLine("\t\t\ttry");
            builder.AppendLine("\t\t\t{");
            builder.AppendLine("\t\t\t\tcmd = new SqlCommand();");
            builder.AppendLine("\t\t\t\tcmd.CommandType = CommandType.StoredProcedure;");
            builder.AppendLine("\t\t\t\tcmd.CommandText = \"usp_" + Program.sulfixInsert + sqlTable.ProcNome + "\";");
            builder.AppendLine();
            foreach (SqlColumn column2 in sqlTable.Colunas)
            {
                builder.AppendLine("\t\t\t\tif (" + column2.Name + "_ == null) cmd.Parameters.AddWithValue(\"@" + column2.Name + "\", DBNull.Value);");
                builder.AppendLine("\t\t\t\telse cmd.Parameters.AddWithValue(\"@" + column2.Name + "\", " + column2.Name + "_);");
                builder.AppendLine();
            }
            builder.AppendLine(Tab(4) + "OpenConnectionTrans();");
            if (!sqlTable.IsTableNo)
            {
                if (str2 != "")
                {
                    if (str == "int")
                    {
                        builder.AppendLine("\t\t\t\t" + str2 + " = Convert.ToInt32(\"0\" + cmd.ExecuteScalar());");
                    }
                    else
                    {
                        builder.AppendLine("\t\t\t\t" + str2 + " = Convert.ToDecimal(\"0\" + cmd.ExecuteScalar());");
                    }
                }
                else
                {
                    builder.AppendLine("\t\t\t\tcmd.ExecuteNonQuery();");
                }
            }
            else
            {
                builder.AppendLine("\t\t\t\tcmd.ExecuteNonQuery().ToString();");
            }
            builder.AppendLine("\t\t\t\treturn sqlTrans;");
            builder.AppendLine("\t\t\t}");
            builder.AppendLine("\t\t\tcatch (SqlException sex){ throw sex; }");
            builder.AppendLine("\t\t\tcatch (Exception ex){ throw ex; }");
            builder.AppendLine("\t\t\tfinally { CloseConnection(); }");
            builder.AppendLine("\t\t}");
            return(builder.ToString());
        }