C# wpf 根据数据库一键生成大量模型类(支持sqllite、Access、Sqlserver)

软件下载

源码下载

使用流程

  1. 存放数据库到对应DataBase文件夹
    C# wpf 根据数据库一键生成大量模型类(支持sqllite、Access、Sqlserver)_第1张图片
  2. 打开软件点击确认
    C# wpf 根据数据库一键生成大量模型类(支持sqllite、Access、Sqlserver)_第2张图片
  3. 在output文件夹可以看到对应模型类
    C# wpf 根据数据库一键生成大量模型类(支持sqllite、Access、Sqlserver)_第3张图片
    C# wpf 根据数据库一键生成大量模型类(支持sqllite、Access、Sqlserver)_第4张图片

MainWindowViewModel

using AutoGenerate.Dal;
using AutoGenerate.Helper;
using GalaSoft.MvvmLight.Command;

using PropertyChanged;
using SqliteTest.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Input;

namespace AutoGenerate.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class MainWindowViewModel 
    {
        private ObservableCollection<string> lisTableName;

        private ObservableCollection<Object> lisBaseModels;

        private ObservableCollection<object> dgvShow;

        private ObservableCollection<string> lisCmbDBNames;

        private Dictionary<string,List<FieldModel>> fields = new Dictionary<string, List<FieldModel>>();

        public ICommand BtnCmd { get; }

        public MainWindowViewModel()
        {
            LisCmbDBNames = new ObservableCollection<string>(GetFilesNames("./DataBase"));

            BtnCmd = new RelayCommand<string>(BtnCMD);
        }

        private void BtnCMD(string obj)
        {
            Singletion<GeneralDal>.Instance.SetDb(new DBSelector(new DBConfig(obj)));
            LisTableName = new ObservableCollection<string>(Singletion<GeneralDal>.Instance.GetTableNames());
            Fields.Clear();
            foreach (string item in LisTableName)
            {
                Fields.Add(item, Singletion<GeneralDal>.Instance.GetFields(item));
            }

            AutoGenerateModel au = new AutoGenerateModel(Singletion<GeneralDal>.Instance.DbName, Fields);
        }

        private static IEnumerable<string> GetFilesNames(string path, string extensions = ".db|.mdf|.mdb")
        {
            string[] tmp = extensions.Split('|');

            return new DirectoryInfo(path).GetFiles("*").Select(x => x.Name)
             .Where(f => extensions.Contains(Path.GetExtension(f).ToLower()));
        }

        public ObservableCollection<object> DgvShow
        {
            get
            {
                return dgvShow;
            }

            set
            {
                dgvShow = value;
            }
        }

        public ObservableCollection<string> LisTableName
        {
            get
            {
                return lisTableName;
            }

            set
            {
                lisTableName = value;
            }
        }

        public ObservableCollection<object> LisBaseModels
        {
            get
            {
                return lisBaseModels;
            }

            set
            {
                lisBaseModels = value;
            }
        }

        public Dictionary<string, List<FieldModel>> Fields
        {
            get
            {
                return fields;
            }

            set
            {
                fields = value;
            }
        }

        public ObservableCollection<string> LisCmbDBNames
        {
            get
            {
                return lisCmbDBNames;
            }

            set
            {
                lisCmbDBNames = value;
            }
        }
    }
}

核心代码

using AutoGenerate.Dal;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AutoGenerate.Helper
{
    class AutoGenerateModel
    {

        public event EventHandler EventModelAutoGenerateSuccess;

        public AutoGenerateModel(string dbName, Dictionary<string, List<FieldModel>> Fields)
        {
            string outputPath = $"./Output/{dbName}/Models";

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            foreach (KeyValuePair<string, List<FieldModel>> item in Fields)
            {
                string fileName = $"Model_{item.Key}.cs";

                string filePath = $"{outputPath}/{fileName}";

                using (StreamWriter sw = File.CreateText(filePath))
                {
                    
                    sw.WriteLine("using System;\r\n");
                    sw.WriteLine("namespace AutoGenerate.Model\r\n{");
                    sw.WriteLine($"\tpublic class Model_{item.Key}: DBModelBase");
                    sw.WriteLine("\t{");

                    foreach (FieldModel item2 in item.Value)
                    {
                        sw.WriteLine("\t\tprivate " + item2.GetStrType()+" _"+item2.FieldName+";\r\n");
                    }

                    foreach (FieldModel item2 in item.Value)
                    {
                        sw.WriteLine("\t\tpublic " + item2.GetStrType() + " " + item2.FieldName + "\r\n\t\t{");
                        sw.WriteLine("\t\t\tget\r\n\t\t\t{");
                        sw.WriteLine($"\t\t\t\treturn _{item2.FieldName};");
                        sw.WriteLine("\t\t\t}");
                        sw.WriteLine("\t\t\tset\r\n\t\t\t{");
                        sw.WriteLine($"\t\t\t\t_{item2.FieldName}=value;");
                        sw.WriteLine("\t\t\t}");
                        sw.WriteLine("\t\t}");
                        sw.WriteLine("");
                    }
                    sw.WriteLine("\t}");
                    sw.WriteLine("}");
                }
            }

            MessageBox.Show("导出成功");

            if(EventModelAutoGenerateSuccess != null)
            {
                EventModelAutoGenerateSuccess(new object(),new EventArgs());
            }
        }
    }
}

你可能感兴趣的:(C#,数据库,sqlserver,c#)