コード例 #1
0
ファイル: frmMain.cs プロジェクト: lufan2008/ProductBoss
        /// <summary>
        /// 生成选中的表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            CheckedListBox.CheckedItemCollection tableNames = this.clbTables.CheckedItems;

            if (tableNames != null && tableNames.Count > 0)
            {
                if (string.IsNullOrEmpty(this.txtNameSpace.Text.Trim()))
                {
                    this.txtNameSpace.Text = this.cmbDBKey.SelectedValue.ToString();
                }

                if (string.IsNullOrEmpty(this.txtPrimaryKey.Text.Trim()))
                {
                    this.txtPrimaryKey.Text = "[PrimaryKey]";
                }

                if (string.IsNullOrEmpty(this.txtIdentity.Text.Trim()))
                {
                    this.txtIdentity.Text = "[Identity]";
                }

                for (int i = 0; i < tableNames.Count; i++)
                {
                    DBTable dbTable = null;

                    AutoGenerateEntityDataAdapter dataAdapter = new AutoGenerateEntityDataAdapter(this.cmbDBKey.SelectedValue.ToString());

                    List<DBTable> list = dataAdapter.GetDBTableDesigner(tableNames[i].ToString());

                    if (list != null && list.Count > 0)
                    {
                        dbTable = list[0];
                    }

                    if (dbTable != null)
                    {
                        List<DBColumn> columns = dataAdapter.GetDBColumnDesigner(dbTable.TableName);

                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("using System;");
                        sb.AppendLine("using System.Collections.Generic;");
                        sb.AppendLine("using System.Linq;");
                        sb.AppendLine("using System.Text;");
                        if (!string.IsNullOrEmpty(this.txtAddNameSpace.Text.Trim()))
                        {
                            sb.AppendLine(this.txtAddNameSpace.Text.Trim());
                        }
                        sb.AppendLine("");
                        sb.AppendLine(string.Format("namespace {0}", this.txtNameSpace.Text.Trim()));
                        sb.AppendLine("{");
                        sb.AppendLine("    /// <summary>");
                        sb.AppendLine("    /// " + dbTable.TableDescription);
                        sb.AppendLine("    /// </summary>");
                        string parentClass = string.IsNullOrEmpty(this.txtParentClass.Text.Trim()) ? "" : " : " + this.txtParentClass.Text.Trim();
                        sb.AppendLine(string.Format("    public partial class {0}{1}", dbTable.TableName, parentClass));
                        sb.AppendLine("    {");
                        sb.AppendLine(string.Format("        public {0}()", dbTable.TableName));
                        sb.AppendLine("        {");
                        sb.AppendLine("        }");

                        if (columns != null && columns.Count > 0)
                        {
                            foreach (DBColumn column in columns)
                            {
                                sb.AppendLine("");
                                if (!string.IsNullOrEmpty(column.ColumnDescription))
                                {
                                    sb.AppendLine("        /// <summary>");
                                    sb.AppendLine("        /// " + column.ColumnDescription);
                                    sb.AppendLine("        /// </summary>");
                                }

                                if (column.IsPrimary.GetValueOrDefault() == 1)
                                {
                                    sb.AppendLine(string.Format("        {0}", this.txtPrimaryKey.Text.Trim()));
                                }

                                if (column.IsIdentity.GetValueOrDefault() == true)
                                {
                                    sb.AppendLine(string.Format("        {0}", this.txtIdentity.Text.Trim()));
                                }

                                string dataType = GetDataTypeByDBType(column.ColumnType);

                                if (column.IsNullable.GetValueOrDefault() == true && !dataType.Equals("object") && !dataType.Equals("string") && !dataType.EndsWith("[]"))
                                {
                                    sb.AppendLine("        public " + dataType + "?" + " " + column.ColumnName + " { get; set; }");
                                }
                                else
                                {
                                    sb.AppendLine("        public " + dataType + " " + column.ColumnName + " { get; set; }");
                                }
                            }
                        }

                        sb.AppendLine("    }");
                        sb.AppendLine("}");

                        string fileName = dbTable.TableName + ".cs";

                        if (string.IsNullOrEmpty(this.txtOutputPath.Text.Trim()))
                        {
                            this.txtOutputPath.Text = System.Windows.Forms.Application.StartupPath.TrimEnd('\\') + "\\";
                        }

                        File.WriteAllText(this.txtOutputPath.Text + fileName, sb.ToString());
                    }
                }

                System.Configuration.Configuration config = System.Reflection.Assembly.GetExecutingAssembly().GetConfiguration();
                config.AppSettings.Settings["DBKey"].Value = this.cmbDBKey.SelectedValue.ToString();
                config.AppSettings.Settings["AddNameSpace"].Value = this.txtAddNameSpace.Text.Trim();
                config.AppSettings.Settings["NameSpace"].Value = this.txtNameSpace.Text.Trim();
                config.AppSettings.Settings["ParentClass"].Value = this.txtParentClass.Text.Trim();
                config.AppSettings.Settings["PrimaryKey"].Value = this.txtPrimaryKey.Text.Trim();
                config.AppSettings.Settings["Identity"].Value = this.txtIdentity.Text.Trim();
                config.AppSettings.Settings["OutputPath"].Value = this.txtOutputPath.Text.Trim();
                config.Save();
                ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
            }
            else
            {
                MessageBox.Show("请选择要生成的表!");
                return;
            }

            MessageBox.Show("生成完成!");
        }
コード例 #2
0
ファイル: frmMain.cs プロジェクト: lufan2008/ProductBoss
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            AutoGenerateEntityDataAdapter dataAdapter = new AutoGenerateEntityDataAdapter(this.cmbDBKey.SelectedValue.ToString());

            List<DBTable> tables = dataAdapter.GetDBTableDesigner(this.txtTableName.Text.Trim());

            this.clbTables.Items.Clear();

            if (tables != null && tables.Count > 0)
            {
                foreach (DBTable item in tables)
                {
                    this.clbTables.Items.Add(item.TableName, CheckState.Checked);
                }
            }
        }