public static string CreateBatEditData(EasyUIModel model)
        {
            StringBuilder batEditContent = new StringBuilder();
            StringBuilder createModel    = new StringBuilder();

            batEditContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtBatEdit{1}\"]);\r\n", model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdStr.ToFirstUpper());
            batEditContent.AppendFormat(@"           List<string> idList = {0}.Split(new char[]{{','}}, StringSplitOptions.RemoveEmptyEntries).ToList();{1}", model.MainKeyIdStr.ToFirstLower(), Environment.NewLine);
            foreach (var item in model.BatEditColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                batEditContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtBatEdit{1}\"]);\r\n", field, attribute);
                createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", attribute, ExtendMethod.ToStringToType(field, item.DBType));
            }

            string template = @"
        private void BatEditData()
        {{
{0}
            {1} model = new {1}();
{2}
            {1}DAL dal = new {1}DAL();
            dal.BatUpdate{1}(idList, model);

            Response.Write(""0"");
        }}
";

            return(string.Format(template, batEditContent.ToString(), model.TableName.ToFirstUpper(), createModel.ToString()));
        }
        public static string CreateEditData(EasyUIModel model)
        {
            StringBuilder editContent = new StringBuilder();
            StringBuilder createModel = new StringBuilder();

            editContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtEdit{1}\"]);\r\n", model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdStr.ToFirstUpper());
            createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", model.MainKeyIdStr.ToFirstUpper(), ExtendMethod.ToStringToType(model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdDBType));
            foreach (var item in model.EditColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                editContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtEdit{1}\"]);\r\n", field, attribute);
                createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", attribute, ExtendMethod.ToStringToType(field, item.DBType));
            }

            string template = @"
        private void EditData()
        {{
{0}
            {1} model = new {1}();
{2}
            {1}DAL dal = new {1}DAL();
            dal.Update{1}(model);

            Response.Write(""0"");
        }}
";

            return(string.Format(template, editContent.ToString(), model.TableName.ToFirstUpper(), createModel.ToString()));
        }
Beispiel #3
0
        public static string CreateGetAllAndPart(EasyUIModel model)
        {
            StringBuilder queryWhereContent = new StringBuilder();
            StringBuilder queryListParams   = new StringBuilder();
            int           index             = 0;

            foreach (var item in model.SearchColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                queryListParams.AppendFormat("{0} {1},", item.DBType.ToMsSqlClassType(), field);

                if (index == 0)
                {
                    queryWhereContent.AppendFormat("            if (!string.IsNullOrEmpty({0}))\r\n", field);
                }
                else
                {
                    queryWhereContent.AppendFormat("            if (!string.IsNullOrEmpty({0}))\r\n", field);
                }

                queryWhereContent.Append("            {\r\n");
                queryWhereContent.AppendFormat("                listParams.Add(new SqlParameter(\"@{0}\", {1}) {{ Value = {2} }});\r\n", item.ColumnName, item.DBType.ToMsSqlDbType(), field);

                queryWhereContent.AppendFormat("                whereStr += \" and {0}=@{0} \";\r\n", item.ColumnName.ToFirstUpper());
                queryWhereContent.Append("            }\r\n");
                queryWhereContent.AppendLine();

                index++;
            }

            StringBuilder selectSqlContent = new StringBuilder();

            selectSqlContent.Append("            string selectSql = string.Format(@\"select * from\r\n");
            selectSqlContent.Append("            " + model.TableName + " where " + model.MainKeyIdStr + " in ({1})\r\n");
            selectSqlContent.Append("            {0};\", whereStr, idArrayStr);\r\n");

            var assignContent = new StringBuilder();

            foreach (var item in model.ColumnList)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                assignContent.AppendFormat("                        model.{0} = sqldr[\"{1}\"] == DBNull.Value ? {2} : {3};\r\n", attribute, item.ColumnName, ExtendMethod.ToDefaultValue(item.DBType), ExtendMethod.ToDefaultDBValue(item.DBType, item.ColumnName));
            }

            string template = @"
        public List<{0}> GetPartAll({1}List<string> idList)
        {{
            var idArrayStr = string.Join("","", (from f in idList
                                select ""'""+ f + ""'"").ToArray());
            string whereStr = string.Empty;
            List<SqlParameter> listParams = new List<SqlParameter>();
{2}
{7}
            List<{0}> result = new List<{0}>();
            using (SqlConnection sqlcn = ConnectionFactory.{3})
            {{
                using(SqlDataReader sqldr = SqlHelper.ExecuteReader(sqlcn, CommandType.Text, selectSql, listParams.ToArray()))
                {{
                    while(sqldr.Read())
                    {{
                        {0} model = new {0}();
{5}
                        result.Add(model);
                    }}
                }}
            }}

            return result;
        }}

        public List<{0}> GetAll({4})
        {{
            string whereStr = string.Empty;
            List<SqlParameter> listParams = new List<SqlParameter>();
{2}
            string selectAllSql = string.Format(""select * from {6} where 1=1 {{0}}"", whereStr);
            List<{0}> result = new List<{0}>();
            using (SqlConnection sqlcn = ConnectionFactory.{3})
            {{
                using(SqlDataReader sqldr = SqlHelper.ExecuteReader(sqlcn, CommandType.Text, selectAllSql, listParams.ToArray()))
                {{
                    while(sqldr.Read())
                    {{
                        {0} model = new {0}();
{5}
                        result.Add(model);
                    }}
                }}
            }}

            return result;
        }}
";

            string queryCountParams = queryListParams.Length > 0 ? queryListParams.ToString().Substring(0, queryListParams.Length - 1) : queryListParams.ToString();

            return(string.Format(template, model.TableName.ToFirstUpper(), queryListParams.ToString(), queryWhereContent.ToString(), model.DbName.ToFirstUpper(), queryCountParams, assignContent.ToString(), model.TableName, selectSqlContent.ToString()));
        }
Beispiel #4
0
        public static string CreateQueryListMethod(EasyUIModel model)
        {
            StringBuilder queryWhereContent = new StringBuilder();
            StringBuilder queryListParams   = new StringBuilder();
            int           index             = 0;

            foreach (var item in model.SearchColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                queryListParams.AppendFormat("{0} {1},", item.DBType.ToMsSqlClassType(), field);

                if (index == 0)
                {
                    queryWhereContent.AppendFormat("if (!string.IsNullOrEmpty({0}))\r\n", field);
                }
                else
                {
                    queryWhereContent.AppendFormat("\t\t\tif (!string.IsNullOrEmpty({0}))\r\n", field);
                }

                queryWhereContent.Append("\t\t\t{\r\n");
                queryWhereContent.AppendFormat("\t\t\t\tlistParams.Add(new SqlParameter(\"@{0}\", {1}) {{ Value = {2} }});\r\n", item.ColumnName, item.DBType.ToMsSqlDbType(), field);

                queryWhereContent.AppendFormat("\t\t\t\twhereStr += \" and {0}=@{0} \";\r\n", item.ColumnName.ToFirstUpper());
                queryWhereContent.Append("\t\t\t}\r\n");
                queryWhereContent.AppendLine();

                index++;
            }

            StringBuilder selectSqlContent = new StringBuilder();

            selectSqlContent.Append("\tstring selectSql = string.Format(@\"select * from\r\n");
            selectSqlContent.AppendFormat("\t        (select top 100 percent *,ROW_NUMBER() over(order by {0}) as rownumber from\r\n", model.MainKeyIdStr);
            selectSqlContent.Append("\t        " + model.TableName + " where 1=1 {0}) as T\r\n");
            selectSqlContent.Append("\t        where rownumber between {1} and {2};\", whereStr, ((page - 1) * pageSize + 1), page * pageSize);\r\n");

            var assignContent = new StringBuilder();

            foreach (var item in model.ColumnList)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                assignContent.AppendFormat("                        model.{0} = sqldr[\"{1}\"] == DBNull.Value ? {2} : {3};\r\n", attribute, item.ColumnName, ExtendMethod.ToDefaultValue(item.DBType), ExtendMethod.ToDefaultDBValue(item.DBType, item.ColumnName));
            }

            string template = @"
        public List<{0}> QueryList({1}int page, int pageSize)
        {{
            string whereStr = string.Empty;
            List<SqlParameter> listParams = new List<SqlParameter>();
            {2}
            {7}
            List<{0}> result = new List<{0}>();
            using (SqlConnection sqlcn = ConnectionFactory.{3})
            {{
                using(SqlDataReader sqldr = SqlHelper.ExecuteReader(sqlcn, CommandType.Text, selectSql, listParams.ToArray()))
                {{
                    while(sqldr.Read())
                    {{
                        {0} model = new {0}();
{5}
                        result.Add(model);
                    }}
                }}
            }}

            return result;
        }}

        public int QueryListCount({4})
        {{
            string whereStr = string.Empty;
            List<SqlParameter> listParams = new List<SqlParameter>();
            {2}
            string selectCountSql = string.Format(""select count(0) from {6} where 1=1 {{0}}"", whereStr);
            using (SqlConnection sqlcn = ConnectionFactory.{3})
            {{
                return Convert.ToInt32(SqlHelper.ExecuteScalar(sqlcn, CommandType.Text, selectCountSql, listParams.ToArray()));
            }}
        }}
";

            string queryCountParams = queryListParams.Length > 0 ? queryListParams.ToString().Substring(0, queryListParams.Length - 1) : queryListParams.ToString();

            return(string.Format(template, model.TableName.ToFirstUpper(), queryListParams.ToString(), queryWhereContent.ToString(), model.DbName.ToFirstUpper(), queryCountParams, assignContent.ToString(), model.TableName, selectSqlContent.ToString()));
        }
Beispiel #5
0
        public static string CreateEditData(BootstrapModel model)
        {
            StringBuilder editContent = new StringBuilder();
            StringBuilder createModel = new StringBuilder();

            editContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtEdit{1}\"]);\r\n", model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdStr.ToFirstUpper());
            createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", model.MainKeyIdStr.ToFirstUpper(), ExtendMethod.ToStringToType(model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdDBType));
            foreach (var item in model.EditColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();
                if (item.DBType.ToLower() == "datetime" ||
                    item.DBType.ToLower() == "date" ||
                    item.DBType.ToLower() == "int" ||
                    item.DBType.ToLower() == "tinyint")
                {
                    createModel.AppendFormat("\t\t\tstring {0}Str = HttpUtility.UrlDecode(Request[\"txtEdit{1}\"]);\r\n", field, attribute);
                    if (item.DBType.ToLower() == "datetime" || item.DBType.ToLower() == "date")
                    {
                        editContent.AppendFormat("\t\t\tDateTime {0} = DateTime.MinValue;\r\n", field);
                        editContent.AppendFormat("\t\t\tDateTime.TryParse({0}Str, out {0});\r\n", field);
                    }
                    else
                    {
                        editContent.AppendFormat("\t\t\tint {0} = 0;;\r\n", field);
                        editContent.AppendFormat("\t\t\tint.TryParse({0}Str, out {0});\r\n", field);
                    }

                    createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", attribute, ExtendMethod.ToStringToType(field, item.DBType));
                }
                else
                {
                    editContent.AppendFormat("\t\t\tstring {0} = HttpUtility.UrlDecode(Request[\"txtEdit{1}\"]);\r\n", field, attribute);
                    createModel.AppendFormat("\t\t\tmodel.{0} = {1};\r\n", attribute, ExtendMethod.ToStringToType(field, item.DBType));
                }
            }

            string template = @"
        private void EditData()
        {{
{0}
            {1} model = new {1}();
{2}
            {1}DAL dal = new {1}DAL();
            dal.Update{1}(model);

            Response.Write(""0"");
        }}
";

            return(string.Format(template, editContent.ToString(), model.TableName.ToFirstUpper(), createModel.ToString()));
        }
        public static string CreateDownAndDownAll(EasyUIModel model)
        {
            StringBuilder searchContent    = new StringBuilder();
            StringBuilder searchStrContent = new StringBuilder();

            foreach (var item in model.SearchColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();

                string name = string.Format("HttpUtility.UrlDecode(Request[\"txtSearch{0}\"])", attribute);
                searchContent.AppendFormat("            {0} {1} = {2};\r\n", item.DBType.ToMsSqlClassType(), field, ExtendMethod.ToStringToType(name, item.DBType));
                searchStrContent.AppendFormat(" {0},", field);
            }

            // 移除最后的逗号
            string searchStrCount = searchStrContent.Length > 0 ? searchStrContent.ToString().Substring(0, searchStrContent.Length - 1) : searchStrContent.ToString();

            StringBuilder batContent = new StringBuilder();

            batContent.AppendFormat("            string ids = HttpUtility.UrlDecode(Request[\"ids\"]);\r\n", model.MainKeyIdStr.ToFirstLower(), model.MainKeyIdStr.ToFirstUpper());
            batContent.Append("            List<string> idList = ids.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries).ToList();\r\n");
            batContent.Append(searchContent.ToString());

            StringBuilder content = new StringBuilder();

            content.Append("<table border='1'><thead><tr>");
            foreach (var item in model.ColumnList)
            {
                content.AppendFormat("<th>{0}</th>", item.Comment);
            }

            content.Append("</tr></thead>");

            StringBuilder appendFormat = new StringBuilder();

            foreach (var item in model.ColumnList)
            {
                appendFormat.AppendFormat(@"                content.AppendFormat(""<td>{{0}}</td>"", list[i].{0});{1}", item.ColumnName.ToFirstUpper(), Environment.NewLine);
            }

            string template = @"
        private void Down()
        {{
{0}
            {1}DAL dal = new {1}DAL();
            List<{1}> data = dal.GetPartAll({4}idList);
            string content = CreateTable(data);
            Response.Clear(); 
            Response.Buffer = true; 
            Response.Charset = ""UTF-8""; 
            Response.AddHeader(""Content-Disposition"", ""attachment; filename={1}.xls""); 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding(""UTF-8""); 
            Response.ContentType = ""application/ms-excel;charset=UTF-8""; 
            Response.Write(content); 
            Response.Flush(); 
            Response.End(); 
        }}

        private void DownAll()
        {{
{0}
            {1}DAL dal = new {1}DAL();
            List<{1}> data = dal.GetAll({5});
            string content = CreateTable(data);
            Response.Clear(); 
            Response.Buffer = true; 
            Response.Charset = ""UTF-8""; 
            Response.AddHeader(""Content-Disposition"", ""attachment; filename={1}.xls""); 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding(""UTF-8""); 
            Response.ContentType = ""application/ms-excel;charset=UTF-8""; 
            Response.Write(content); 
            Response.Flush(); 
            Response.End(); 
        }}

        private string CreateTable(List<{1}> list)
        {{
            StringBuilder content = new StringBuilder();
            
            // create columns header
            content.Append(""{2}"");
            for (int i = 0, len = list.Count; i < len; i++)
            {{
                content.Append(""<tr>"");
{3}
                content.Append(""</tr>"");
            }}
            content.Append(""</tbody></table>"");

            return content.ToString();            
        }}";

            return(string.Format(template, batContent.ToString(), model.TableName.ToFirstUpper(), content.ToString(), appendFormat.ToString(), searchStrContent.ToString(), searchStrCount));
        }
        public static string CreateLoadData(EasyUIModel model)
        {
            string        template         = @"
        private void LoadData()
        {{
            int page = Convert.ToInt32(Request.Form[""page""]);
            int pageSize = Convert.ToInt32(Request.Form[""pageSize""]);
{0}
            {1}DAL dal = new {1}DAL();
            var list = dal.QueryList({2}page, pageSize);
{4}
            int itemCount = dal.QueryListCount({3});
            int pageCount = (int)Math.Ceiling((double)itemCount / (double)pageSize);
			JavaScriptSerializer js = new JavaScriptSerializer();
            var str = js.Serialize(new {{ PageCount = pageCount, ItemCount = itemCount, Data = list }});
            Response.Write(str);
        }}
";
            StringBuilder searchContent    = new StringBuilder();
            StringBuilder searchStrContent = new StringBuilder();

            foreach (var item in model.SearchColumns)
            {
                string field     = item.ColumnName.ToFirstLower();
                string attribute = item.ColumnName.ToFirstUpper();

                string name = string.Format("HttpUtility.UrlDecode(Request[\"txtSearch{0}\"])", attribute);
                searchContent.AppendFormat("\t\t\t{0} {1} = {2};\r\n", item.DBType.ToMsSqlClassType(), field, ExtendMethod.ToStringToType(name, item.DBType));
                searchStrContent.AppendFormat(" {0},", field);
            }

            StringBuilder encodeContent = new StringBuilder();

            foreach (var item in model.ColumnList)
            {
                if (item.DBType == "varchar")
                {
                    string attribute = item.ColumnName.ToFirstUpper();

                    encodeContent.AppendLine(string.Format("\t\t\tlist.ForEach(p => p.{0} = HttpUtility.HtmlEncode(p.{0}));", attribute));
                }
            }

            // 移除最后的逗号
            string searchStrCount = searchStrContent.Length > 0 ? searchStrContent.ToString().Substring(0, searchStrContent.Length - 1) : searchStrContent.ToString();

            return(string.Format(template, searchContent.ToString(), model.TableName.ToFirstUpper(), searchStrContent.ToString(), searchStrCount, encodeContent.ToString()));
        }