Пример #1
0
        public string GenerateModel(DataTable columns, string tableType = "U")
        {
            if (columns.Rows.Count > 0)
            {
                string xmlName   = Application.StartupPath.ToString().Replace("\\bin\\Debug", "\\Support\\Constructor") + "\\Template.xml";
                string codeStr   = string.Empty;
                string tableName = columns.Rows[0]["tableName"].ToString();
                string varStr    = string.Empty;
                string setGetStr = string.Empty;
                foreach (DataRow column in columns.Rows)
                {
                    string type         = ConvertType.SqlTypeString2CsharpTypeString(column["sqlType"].ToString());
                    string temp         = XmlReader.GetStr(xmlName, "Template", "Model", "Var");
                    string variable     = CheckKeywords.InCSharp(column["name"].ToString());
                    string defaultValue = string.IsNullOrEmpty(column["defaultValue"].ToString()) ? "" : column["defaultValue"].ToString().TrimStart('(').TrimEnd(')');
                    if (defaultValue.ToLower().Contains("getdate"))
                    {
                        defaultValue = "DateTime.Now";
                        if (!column["sqlType"].ToString().Contains("date"))
                        {
                            defaultValue = "DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss.fff\")";
                        }
                    }
                    else if (defaultValue.ToLower().Contains("newid"))
                    {
                        defaultValue = "Guid.NewGuid()";
                    }
                    else if (column["sqlType"].ToString().Contains("date") && column["allowNull"].ToString() == "1")
                    {
                        type        += "?";
                        defaultValue = "null";
                    }
                    else
                    {
                        defaultValue = defaultValue.Replace("'", "\"");
                    }

                    varStr    += string.Format(temp, type, string.IsNullOrEmpty(defaultValue) ? variable : variable + " = " + defaultValue);
                    temp       = XmlReader.GetStr(xmlName, "Template", "Model", "SetGet");
                    setGetStr += string.Format(temp, variable + " " + column["description"].ToString(), type, variable);
                }
                string bodyCodeStr = varStr + setGetStr;

                string tableDescription = columns.Rows[0]["tableDescription"].ToString();
                codeStr += string.Format(XmlReader.GetStr(xmlName, "Template", "Model", "Head"), Settings.Default.Namespace, tableName + " " + tableDescription, tableName, bodyCodeStr, DateTime.Now);
                return(codeStr.Replace("@", "{").Replace("$", "}").Replace("\n            ", "\n").Trim());
            }
            else
            {
                return("");
            }
        }
Пример #2
0
        public string GenerateUpdate(DataTable tables)
        {
            string xmlName = Application.StartupPath.ToString().Replace("\\bin\\Debug", "\\Support\\Constructor") + "\\Template.xml";
            string codeStr = string.Empty;
            string temp    = string.Empty;

            foreach (DataRow table in tables.Rows)
            {
                string    tableName = table["name"].ToString();
                DataTable columns   = Support.DataBaseHandler.Select.GetColumnInfo(tableName);
                if (columns.Rows.Count > 0)
                {
                    string tableNameUp = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(tableName);
                    string tableDesc   = columns.Rows[0]["tableDescription"].ToString();
                    string primaryKey  = columns.Rows.Count > 0 ? columns.Rows[0]["name"].ToString() : string.Empty;

                    string summaryParam = string.Format(XmlReader.GetStr(xmlName, "Template", "Update", "SummaryParam"), tableNameUp, tableDesc);
                    string body         = string.Empty;
                    string values       = string.Empty;
                    foreach (DataRow column in columns.Rows)
                    {
                        string variable = CheckKeywords.InCSharp(column["name"].ToString());
                        if (column["isPrimaryKey"].ToString() == "1")
                        {
                            primaryKey = variable;
                        }
                        else if (column["name"].ToString().ToUpper().Contains("ID"))
                        {
                            primaryKey = column["name"].ToString();
                        }
                        if (column["sqlType"].ToString().Contains("date"))
                        {
                            values += "_" + tableName + "." + variable + " == null ? \"NULL\" : _" + tableName + "." + variable + ".ToString(), ";
                        }
                        else
                        {
                            values += "_" + tableName + "." + variable + ", ";
                        }
                    }
                    primaryKey = CheckKeywords.InCSharp(primaryKey);
                    values    += "_" + tableName + "." + primaryKey;
                    body       = string.Format(XmlReader.GetStr(xmlName, "Template", "Update", "Body"), tableName + " " + tableDesc, summaryParam, tableNameUp, tableName, values);
                    temp      += body;
                }
            }
            codeStr = string.Format(XmlReader.GetStr(xmlName, "Template", "Update", "Head"), Settings.Default.Namespace, DateTime.Now, temp);
            return(codeStr.Replace("@", "{").Replace("$", "}").Replace("\n            ", "\n").Trim());
            //Int32? dd = null;
            //return string.Format("nullTest = {0}", dd==null?"NULL":dd.ToString());
        }
Пример #3
0
        public string GenerateSelect(DataTable tables)
        {
            string xmlName   = Application.StartupPath.ToString().Replace("\\bin\\Debug", "\\Support\\Constructor") + "\\Template.xml";
            string codeStr   = string.Empty;
            string temp      = string.Empty;
            string tableDesc = string.Empty;

            foreach (DataRow table in tables.Rows)
            {
                string    tableName = table["name"].ToString();
                string    tableType = table["xtype"].ToString().Trim().ToUpper();
                DataTable columns   = Support.DataBaseHandler.Select.GetColumnInfo(tableType, tableName);
                if (columns.Rows.Count > 0)
                {
                    string primaryKey     = columns.Rows[0]["name"].ToString();
                    string primaryKeyDesc = columns.Rows.Count > 0 ? columns.Rows[0]["description"].ToString() : string.Empty;
                    //string primaryKeyType = columns.Rows.Count > 0 ? ConvertType.SqlTypeString2CsharpTypeString(columns.Rows[0]["sqlType"].ToString()) : string.Empty;
                    tableDesc = columns.Rows[0]["tableDescription"].ToString();
                    string summaryParam = string.Empty;
                    string body         = string.Empty;
                    foreach (DataRow column in columns.Rows)
                    {
                        if (column["isPrimaryKey"].ToString() == "1")
                        {
                            primaryKey     = column["name"].ToString();
                            primaryKeyDesc = column["description"].ToString();
                            //primaryKeyType = ConvertType.SqlTypeString2CsharpTypeString(column["sqlType"].ToString());
                            //primaryKeyType = "string";
                        }
                        else if (column["name"].ToString().ToUpper().Contains("ID"))
                        {
                            primaryKey = column["name"].ToString();
                        }
                    }
                    primaryKey   = CheckKeywords.InCSharp(primaryKey);
                    summaryParam = string.Format(XmlReader.GetStr(xmlName, "Template", "Select", "SummaryParam"), primaryKey, primaryKeyDesc);
                    string tableNameUp = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(tableName);
                    body  = string.Format(XmlReader.GetStr(xmlName, "Template", "Select", "Body"), tableName + " " + tableDesc, summaryParam, tableNameUp, primaryKey);
                    temp += body;
                }
            }
            codeStr = string.Format(XmlReader.GetStr(xmlName, "Template", "Select", "Head"), Settings.Default.Namespace, DateTime.Now, temp);
            return(codeStr.Replace("@", "{").Replace("$", "}").Replace("\n            ", "\n").Trim());
        }