Beispiel #1
0
        public bool Write(FileStream fs, EntityPropity headerOfEntity)
        {
            List <Column> columns = headerOfEntity.ColumnsPropoty;
            //向流中写入字符
            StreamWriter sw = new StreamWriter(fs);

            //写入类的头部信息
            sw.WriteLine("/*\n" + "*作者:" + headerOfEntity.ClassAuthor + "\n" + "*创建时间:" + DateTime.Now.ToString() + " \n" + "*/\n");
            sw.WriteLine("using Snt.Framework.DataAttribute;");
            sw.WriteLine("using Snt.Framework.Entities;");
            sw.WriteLine("using System;" + "\n");
            //判断命名空间
            if (headerOfEntity.NameSpace != "")
            {
                //写入命名空间
                sw.WriteLine("namespace " + headerOfEntity.NameSpace + "\n{");
                sw.WriteLine("    [Table(" + "\"" + headerOfEntity.ClassName + "\"" + ")]");
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("    [Column(new string[]{" + "\"" + columns[0].Name + "\"}");
                for (int i = 1; i < columns.Count - 1; i++)
                {
                    stringBuilder.AppendFormat(",{0}{1}{2}", "\"", columns[i].Name, "\"");
                }
                sw.WriteLine(stringBuilder.ToString() + ")]");
                stringBuilder.Clear();
                //写入类说明
                sw.WriteLine("    /// <summary>\n" + "    /// " + headerOfEntity.ClassExplain + "\n    /// </summary>");
                //写类的定义
                sw.WriteLine("    public class " + headerOfEntity.ClassName + ":EntityBase");
                sw.WriteLine("    {");
                for (int i = 0; i < columns.Count - 1; i++)
                {
                    stringBuilder.AppendFormat("       public const string {0} = {1}{2}{3};{4}", columns[i].Name.ToUpper(), "\"", columns[i].Name, "\"", "\n");
                }
                sw.WriteLine(stringBuilder.ToString());
                //写属性
                for (int i = 0; i < columns.Count - 1; i++)
                {
                    sw.WriteLine("       private " + columns[i].Type + " " + FirstToLower(columns[i].Name) + ";\n" + "       /// <summary>\n" + "       /// " + columns[i].Description + "\n" +
                                 "       /// </summary>\n" +
                                 "       public " + columns[i].Type + " " + columns[i].Name + "\n" +
                                 "       {\n" +
                                 "         get { return " + FirstToLower(columns[i].Name) + "; }\n" +
                                 "         set { " + FirstToLower(columns[i].Name) + " = value; }\n" +
                                 "       }\n");
                }
                sw.Write("    }\n");
                sw.WriteLine("}");
            }
            sw.Close();
            fs.Close();
            return(true);
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            List <EntityPropity> entityPropities = new List <EntityPropity>();
            string        connection             = $"Data Source={txtServerName.Text};Initial Catalog={cobmDatabaseName.SelectedItem.ToString()};User ID={txtUserName.Text};Password={txtPwd.Text}";
            SqlConnection sqlConnection          = new SqlConnection(connection);

            if (sqlConnection.State != ConnectionState.Connecting)
            {
                for (int i = 0; i < listCheckBox.Count; i++)
                {
                    sqlConnection.Open();
                    // SELECT TableName = OBJECT_NAME(c.object_id),ColumnsName = c.name,Description = ex.value,ColumnType = t.name,Length = c.max_length FROM sys.columns c LEFT OUTER JOIN sys.extended_properties ex ON ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description'LEFT OUTER JOIN systypes t ON c.system_type_id = t.xtype WHERE OBJECTPROPERTY(c.object_id, 'IsMsShipped') = 0 AND OBJECT_NAME(c.object_id ) = 'StorageLocation'
                    string        commamdText = $"SELECT TableName = OBJECT_NAME(c.object_id),ColumnsName = c.name,Description = ex.value,ColumnType = t.name,Length = c.max_length FROM sys.columns c LEFT OUTER JOIN sys.extended_properties ex ON ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description'LEFT OUTER JOIN systypes t ON c.system_type_id = t.xtype WHERE OBJECTPROPERTY(c.object_id, 'IsMsShipped') = 0 AND OBJECT_NAME(c.object_id ) = '{listCheckBox[i].Name}'";
                    SqlCommand    sqlCommand  = new SqlCommand(commamdText, sqlConnection);
                    SqlDataReader reader      = sqlCommand.ExecuteReader();
                    List <Column> lis         = new List <Column>();
                    while (reader.Read())
                    {
                        Column column = new Column();
                        column.Name        = reader["ColumnsName"].ToString();
                        column.Description = reader["Description"].ToString();
                        switch (reader["ColumnType"].ToString())
                        {
                        case "int":
                            column.Type = "int";
                            break;

                        case "float":
                            column.Type = "float";
                            break;

                        case "datetime":
                            column.Type = "DateTime";
                            break;

                        default:
                            column.Type = "string";
                            break;
                        }
                        lis.Add(column);
                    }
                    EntityPropity entityPropity = new EntityPropity();
                    entityPropity.ClassAuthor    = "";
                    entityPropity.ClassExplain   = "";
                    entityPropity.ClassName      = listCheckBox[i].Name;
                    entityPropity.ColumnsPropoty = lis;
                    entityPropity.NameSpace      = txtNamespace.Text;
                    entityPropities.Add(entityPropity);
                    sqlConnection.Close();
                }
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "将实体类保存到:";
                IWriteEntitys writeEntitys = new WriteEntity();
                //设置保存窗口的显示
                DialogResult result = dialog.ShowDialog();
                bool         res    = true;
                if (result == DialogResult.OK)
                {
                    for (int i = 0; i < entityPropities.Count; i++)
                    {
                        FileStream fs = new FileStream(dialog.SelectedPath + "\\" + entityPropities[i].ClassName + ".cs", FileMode.Create, FileAccess.Write);
                        res &= writeEntitys.Write(fs, entityPropities[i]);
                    }
                    if (res)
                    {
                        MessageBox.Show("保存成功!");
                    }
                }
            }
        }