Example #1
0
        private void btnGenerator_Click(object sender, EventArgs e)
        {
            #region 資料驗證

            if (this.gvSelect.Rows.Count == 0)
            {
                MessageBox.Show("欄位列表最少需有一筆資料", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrWhiteSpace(this.tbxClassName.Text))
            {
                MessageBox.Show("請輸入ClassName", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            #endregion 資料驗證

            var pocoHelper = new PocoHelpers().GetPocoHelper(ddlDbType.Text);

            StringBuilder sb = new StringBuilder();
            var spColumns = new StringBuilder();
            var spConditions = new StringBuilder();
            var spParamters = new StringBuilder();
            var spUpdateColumns = new StringBuilder();

            string requiredTipStr = ConfigHelper.RequiredStr;
            string maxLengthTipStr = ConfigHelper.MaxLengthStr;

            if (cbxFormatErrorMsg.Checked)
            {
                requiredTipStr = "{0}" + requiredTipStr;
                maxLengthTipStr = "{0}" + maxLengthTipStr;
            }

            #region Property 文字產生

            foreach (DataGridViewRow rowItem in this.gvSelect.Rows)
            {
                var dbColumnName = Convert.ToString(rowItem.Cells[0].Value);
                var dbType = Convert.ToString(rowItem.Cells[1].Value);
                var dbMaxLength = Convert.ToString(rowItem.Cells[2].Value);
                var dbNuable = Convert.ToString(rowItem.Cells[3].Value) == "Y" ||
                               Convert.ToString(rowItem.Cells[3].Value).ToUpper() == "TRUE";
                var dbDesc = Convert.ToString(rowItem.Cells[5].Value);
                var dbScale = Convert.ToString(rowItem.Cells[6].Value);
                var dbPrecision = Convert.ToString(rowItem.Cells[7].Value);
                var dbIsIdentity = Convert.ToBoolean(rowItem.Cells[8].Value);

                #region SP產生

                spColumns.Append(dbColumnName + " ,");
                spConditions.Append("@" + dbColumnName + " ,");

                var spParamter = $"@{dbColumnName} {dbType.ToUpper()}";
                if (!string.IsNullOrWhiteSpace(dbMaxLength))
                {
                    var max = dbMaxLength == "-1" ? "MAX" : dbMaxLength;
                    spParamter += $"({max})";
                }
                spParamter += " ,";
                spParamters.AppendLine(spParamter);

                spUpdateColumns.AppendLine($"{dbColumnName} = @{dbColumnName} ,");

                #endregion

                // 轉換資料型態對應
                string dataType = pocoHelper.GetDataType(dbType, dbScale, dbPrecision);

                // Nullable
                if (dbNuable && !new List<string>() {"object", "string", "byte[]"}.Contains(dataType))
                    dataType = String.Format("{0}?", dataType);

                //Summary
                sb.AppendLine("\t\t///<summary>");
                sb.AppendLine(string.Format("\t\t///{0}", (string.IsNullOrWhiteSpace(dbDesc) ? dbColumnName : dbDesc)));
                sb.AppendLine("\t\t///</summary>");

                //DB自動編號產生Remarks
                if (dbIsIdentity)
                    sb.AppendLine("\t\t///<remarks>Identity Specification Is Identity</remarks>");

                // Validate Attribute
                if (this.cbxValidateAttr.Checked)
                {
                    sb.AppendLine(string.Format("\t\t[Display(Name = \"{0}\")]", dbDesc));

                    if (dbColumnName.ToLower().Contains("url"))
                        sb.AppendLine("\t\t[Url]");

                    if (dbColumnName.ToLower().Contains("email"))
                        sb.AppendLine("\t\t[EmailAddress]");

                    if (dbColumnName.ToLower().Contains("datetime") || dbType.ToLower().Contains("datetime"))
                        sb.AppendLine(
                            "\t\t[DisplayFormat(DataFormatString = \"{0:yyyy/MM/dd HH:mm}\", ApplyFormatInEditMode = true)]");
                    else if (dbColumnName.ToLower().Contains("date") || dbType.ToLower().Contains("date"))
                        sb.AppendLine(
                            "\t\t[DisplayFormat(DataFormatString = \"{0:yyyy/MM/dd}\", ApplyFormatInEditMode = true)]");

                    if (!dbNuable && !dbIsIdentity)
                        sb.AppendLine(string.Format("\t\t[Required(ErrorMessage = \"{0}\")]", requiredTipStr));

                    if (!string.IsNullOrWhiteSpace(dbMaxLength) && dbMaxLength != "-1")
                        sb.AppendLine(string.Format("\t\t[StringLength({0}, ErrorMessage = \"{1}{0}。\")]",
                            dbMaxLength,
                            maxLengthTipStr));
                }

                //Property
                sb.AppendLine(string.Format("\t\tpublic {0} {1} {{ get; set; }}",
                    dataType,
                    (cbxOriColumnName.Checked ? dbColumnName : dbColumnName.FormatColumnName())
                    ));

                sb.AppendLine("");
            }

            #endregion Property 文字產生

            //載入Template
            this.rtbTemplete.LoadFile(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Template.Model.txt"),
                RichTextBoxStreamType.PlainText);
            this.rtbTemplete.Text = this.rtbTemplete.Text.Replace("<<className>>", tbxClassName.Text);
            this.rtbTemplete.Text = this.rtbTemplete.Text.Replace("<<propertyBlock>>", sb.ToString());

            #region 產生Insert SP

            this.rtbxInsertSP.LoadFile(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Template.InsertSP.txt"),
                RichTextBoxStreamType.PlainText);

            this.rtbxInsertSP.Text = this.rtbxInsertSP.Text.Replace("<<className>>", tbxClassName.Text);
            this.rtbxInsertSP.Text = this.rtbxInsertSP.Text.Replace("<<procedureColumns>>", spColumns.ToString().TrimEnd(','));
            this.rtbxInsertSP.Text = this.rtbxInsertSP.Text.Replace("<<procedureCondition>>", spConditions.ToString().TrimEnd(','));
            this.rtbxInsertSP.Text = this.rtbxInsertSP.Text.Replace("<<procedureParameters>>", spParamters.ToString().Trim().TrimEnd(','));

            #endregion

            #region 產生Update SP

            this.rtbxUpdateSP.LoadFile(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Template.UpdateSP.txt"),
                RichTextBoxStreamType.PlainText);

            this.rtbxUpdateSP.Text = this.rtbxUpdateSP.Text.Replace("<<className>>", tbxClassName.Text);
            this.rtbxUpdateSP.Text = this.rtbxUpdateSP.Text.Replace("<<UpdateColumns>>", spUpdateColumns.ToString().Trim().TrimEnd(','));
            this.rtbxUpdateSP.Text = this.rtbxUpdateSP.Text.Replace("<<procedureParameters>>", spParamters.ToString().Trim().TrimEnd(','));

            #endregion

            #region 產生分頁Query SP

            this.rbxPagingQuery.LoadFile(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "Template.GetPagingtSP.txt"),
                RichTextBoxStreamType.PlainText);

            var firstColumnName = gvSelect.Rows[0].Cells[0].Value.ToString();

            this.rbxPagingQuery.Text = this.rbxPagingQuery.Text.Replace("<<className>>", tbxClassName.Text);
            this.rbxPagingQuery.Text = this.rbxPagingQuery.Text.Replace("<<FirstColumn>>", firstColumnName);
            this.rbxPagingQuery.Text = this.rbxPagingQuery.Text.Replace("<<procedureColumns>>", spColumns.ToString().Trim().TrimEnd(','));

            #endregion

            #region 產出檔案

            if (cbxGenFile.Checked)
            {
                var basePath = string.IsNullOrWhiteSpace(tbxoutputPath.Text)
                    ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OutputFile")
                    : tbxoutputPath.Text.Trim();

                var path = Path.Combine(basePath, tbxClassName.Text + ".cs");
                using (StreamWriter outfile = new StreamWriter(path))
                {
                    outfile.Write(this.rtbTemplete.Text);
                }
            }

            #endregion 產出檔案

            this.tabControl1.SelectedIndex = 1;
        }
Example #2
0
        private void gvTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                var helper = new PocoHelpers().GetPocoHelper(ddlDbType.Text, GetDBInfo());

                string tableID = this.gvTable.Rows[e.RowIndex].Cells["QueryKey"].Value.ToString();
                gvColumn.DataSource = helper.GetColumnList(tableID);

                if (cbxSingleTable.Checked)
                {
                    tbxClassName.Text = this.gvTable.Rows[e.RowIndex].Cells["TableName"].Value.ToString();
                    btnAllDown_Click(sender, e);
                }
            }
        }
Example #3
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                var dbInfo = GetDBInfo();
                var helper = new PocoHelpers().GetPocoHelper(ddlDbType.Text, dbInfo);
                gvTable.DataSource = helper.GetTableList();

                ConfigHelper.UpdateDbInfo(dbInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }