Example #1
0
        //生成
        private void btn_save_Click(object sender, EventArgs e)
        {
            ConfigModel configModel = new ConfigModel();

            configModel.MARK          = "@";                  //SQL的连接类型
            configModel.str_nameSpace = textBox1.Text.Trim(); //命名空间
            configModel.str_SqlHelper = textBox2.Text.Trim(); //数据访问类名

            configModel.str_ExecuteNonQuery = textBox5.Text.Trim();
            configModel.str_UpdateDatabase  = textBox6.Text.Trim();
            configModel.str_ExecuteScalar   = textBox4.Text.Trim();
            configModel.str_GetDataTable    = textBox7.Text.Trim();
            configModel.str_Exists          = textBox10.Text.Trim();
            configModel.str_GetModel        = textBox8.Text.Trim();
            configModel.str_GetModelList    = textBox9.Text.Trim();

            #region 生成文件夹

            //string file_BLL = textBox3.Text.Trim();
            //string file_DAL = textBox11.Text.Trim();
            //string file_IDAL = textBox12.Text.Trim();
            //string file_Model = textBox13.Text.Trim();

            //if (!Directory.Exists(file_Model))
            //{
            //    Directory.CreateDirectory(file_Model);
            //}
            //if (!Directory.Exists(file_IDAL))
            //{
            //    Directory.CreateDirectory(file_IDAL);
            //}
            //if (!Directory.Exists(file_BLL))
            //{
            //    Directory.CreateDirectory(file_BLL);
            //}
            //if (!Directory.Exists(file_DAL))
            //{
            //    Directory.CreateDirectory(file_DAL);
            //}

            //if (comboBox1.SelectedIndex == 1)
            //{
            //    string path = textBox3.Text.Trim();
            //    if (!string.IsNullOrEmpty(path))
            //    {
            //        if (path.Contains("\\BLL"))
            //        {
            //            path = path.Replace("\\BLL", "");

            //            string BaseBLLSrc = Directory.GetCurrentDirectory() + "/BaseBll.cs";
            //            string BaseDALSrc = Directory.GetCurrentDirectory() + "/BaseDAL.cs";
            //            string BaseIDALSrc = Directory.GetCurrentDirectory() + "/BaseIDAL.cs";

            //            File.Copy(BaseBLLSrc, path + "\\BaseBll.cs");
            //            File.Copy(BaseDALSrc, path + "\\BaseDAL.cs");
            //            File.Copy(BaseIDALSrc, path + "\\BaseIDAL.cs");
            //        }
            //    }
            //}

            #endregion 生成文件夹

            string str_CreateMap     = "";
            string str_SetObjectName = "";
            string str_SetPrimaryKey = "";
            string str_AddColumn     = "";

            List <string> listTableName = new List <string>();

            //遍历每个表
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                string str_table = listBox2.Items[i].ToString();//表名

                //string str_BLLName = str_table + textBox17.Text.Trim();
                //string str_DALName = str_table + textBox16.Text.Trim();
                //string str_IDALName = str_table + textBox15.Text.Trim();
                //string str_ModelName = str_table + textBox14.Text.Trim();

                string sql = @"
SELECT
    表名       = case when a.colorder=1 then d.name else '' end,
    表说明     = case when a.colorder=1 then isnull(f.value,'') else '' end,
    字段序号   = a.colorder,
    columnName     = a.name,
    标识       = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 'Y'else 'N' end,
    primaryKey       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
                     SELECT name FROM sysindexes WHERE indid in(
                        SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then 'Y' else 'N' end,
    columnType       = b.name,
    占用字节数 = a.length,
    char_col_decl_length       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
    小数位数   = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
    nullable     = case when a.isnullable=1 then 'N'else 'Y' end,
    data_default     = isnull(e.text,''),
    columnComment   = isnull(g.[value],'')
FROM  syscolumns a
left join systypes b on a.xusertype=b.xusertype
inner join sysobjects d on     a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
left join syscomments e on     a.cdefault=e.id
left join sys.extended_properties g on     a.id=g.major_id and a.colid=g.minor_id
left join    sys. extended_properties f on     d.id=f.major_id and f.major_id=0
where     d.name=" + configModel.MARK + "a order by     a.id,a.colorder";

                SqlParameter[] sps       = { new SqlParameter(configModel.MARK + "a", str_table) };
                DataTable      dt_tables = SqlHelper.GetDataTable(sql, sps);

                if (dt_tables == null)
                {
                    return;
                }
                if (dt_tables.Rows.Count > 0)
                {
                    listTableName.Add(str_table);
                    #region 生成内容

                    StringBuilder sb_column1 = new StringBuilder(); //格式如 NO_ID,ST_NAME,ST_VALUES,NO_ORDER,ST_OTHER,ST_VALUES_ENG
                    StringBuilder sb_column2 = new StringBuilder(); //格式如 @NO_ID,@ST_NAME,@ST_VALUES,@NO_ORDER,@ST_OTHER,@ST_VALUES_ENG
                    StringBuilder sb_column3 = new StringBuilder(); //格式如 new SqlParameter("@NO_ID", SqlType.Number,4),
                    StringBuilder sb_column4 = new StringBuilder(); //格式如 parameters[0].Value = model.NO_ID;
                    StringBuilder sb_column5 = new StringBuilder(); //格式如 strSql.Append("ST_NAME=@ST_NAME,");
                    for (int j = 0; j < dt_tables.Rows.Count; j++)
                    {
                        string colName = dt_tables.Rows[j]["columnName"].ToString().Trim();                 //字段名
                        string colType = CommonCode.GetColType(dt_tables.Rows[j]["columnType"].ToString()); //字段类型
                        if (j == dt_tables.Rows.Count - 1)
                        {
                            sb_column1.Append(colName);
                            sb_column2.Append(configModel.MARK + colName);
                            sb_column3.AppendLine("					new SqlParameter(\""+ configModel.MARK + colName + "\", model." + colName + ")};");
                            //sb_column4.AppendLine("            parameters[" + j + "].Value = model." + colName + ";");
                            sb_column4.AppendLine("item." + colName + "=model." + colName + ";");
                            sb_column5.AppendLine("            strSql.Append(\"" + colName + "=" + configModel.MARK + colName + "\");");
                        }
                        else
                        {
                            sb_column1.Append(colName + ",");
                            sb_column2.Append(configModel.MARK + colName + ",");
                            sb_column3.AppendLine("					new SqlParameter(\""+ configModel.MARK + colName + "\", model." + colName + "),");
                            //sb_column4.AppendLine("            parameters[" + j + "].Value = model." + colName + ";");
                            sb_column4.AppendLine("item." + colName + "=model." + colName + ";");
                            sb_column5.AppendLine("            strSql.Append(\"" + colName + "=" + configModel.MARK + colName + ",\");");
                        }
                    }

                    #endregion 生成内容

                    ////生成model
                    //new CreateModel().Create(file_Model, configModel.str_nameSpace, dt_tables, str_ModelName);

                    ////生成IDAL
                    //new CreateIDAL().Create(file_IDAL, configModel.str_nameSpace, str_IDALName, str_ModelName);

                    ////生成BLL
                    //new CreateBLL().Create(file_BLL, configModel.str_nameSpace, str_table, str_BLLName, str_IDALName, str_ModelName);

                    ////生成DAL
                    //new CreateDAL().Create(file_DAL, str_table, str_DALName, str_IDALName, str_ModelName, sb_column1, sb_column2, sb_column3, sb_column4, sb_column5, configModel);


                    //new Create_DeleteCmdDto().Create(configModel.str_nameSpace, dt_tables, str_table);
                    //new Create_SaveCmdDto().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_CmdDto().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Dto().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_FilterDto().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Query().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_ViewModel().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_FilterViewModel().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Entity().Create(configModel.str_nameSpace, dt_tables, str_table);

                    new Create_Repository().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_IRepository().Create(configModel.str_nameSpace, dt_tables, str_table);

                    new Create_DomainService().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Business().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_IDataAccess().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_DataAccess().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_IService().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Service().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_IBusiness().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_DomainModel().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Web_Page().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Web_Controller().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Sql().Create(configModel.str_nameSpace, dt_tables, str_table);
                    new Create_Json().Create(configModel.str_nameSpace, dt_tables, str_table);

                    if (checkBox1.Checked)
                    {
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Business");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "BusinessInterface");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "DataAccess");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "DataAccessInterface");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Domain");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Dto");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Entity");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Query");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Repository");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Service");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "ServiceInterface");
                        new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "ViewModel");
                        //new Create_csproj().Create(configModel.str_nameSpace, dt_tables, str_table, "Web");
                    }

                    str_CreateMap += new Create_Config().GetStr_CreateMap(configModel.str_nameSpace, dt_tables, str_table);

                    str_SetObjectName += new Create_Config().GetStr_SetObjectName(configModel.str_nameSpace, dt_tables, str_table);

                    str_SetPrimaryKey += new Create_Config().GetStr_SetPrimaryKey(configModel.str_nameSpace, dt_tables, str_table);

                    str_AddColumn += new Create_Sql().GetSql_AddColumn(str_table, "IsDelete", "bit not null default(0)", "是否删除");
                }
                label4.Text = "提示信息:" + (i + 1) + "个文件,全部生成成功!" + DateTime.Now;
            }


            string filePath = "C:\\Code\\Config";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }


            CommonCode.Save(filePath + "/AutoMapMapper CreateMap.txt", str_CreateMap);
            CommonCode.Save(filePath + "/DbConfig SetObjectName.txt", str_SetObjectName);
            CommonCode.Save(filePath + "/DbConfig SetPrimaryKey.txt", str_SetPrimaryKey);
            CommonCode.Save(filePath + "/AddColumn.txt", str_AddColumn);

            if (checkBox3.Checked)
            {
                Create_Config.SaveAutoMapMapper(str_CreateMap);
                Create_Config.SaveDBConfig(str_SetObjectName, str_SetPrimaryKey);
            }

            if (checkBox2.Checked)
            {
                new Create_Enum().Create(configModel.str_nameSpace, listTableName);
            }

            if (textBox3.Text != "")
            {
                var arr = textBox3.Text.Split(',');
                if (arr[0] != "ControllerCode")
                {
                    StringBuilder sb_body = new StringBuilder();
                    Create_Sql.SetData2(arr[0], arr[1], arr[2], sb_body);

                    filePath = "C:\\Code\\Sql";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    CommonCode.Save(filePath + "/生成的.txt", sb_body.ToString());
                    CommonCode.Save(filePath + "/生成的_Delete.txt", Create_Sql.GetSql_Delete(arr[2]));
                }
            }
            //OpenFolder();
        }