示例#1
0
        /// <summary>
        /// 预览 Model
        /// </summary>
        /// <param name="tableName"></param>
        private void showModelPreview(string tableName)
        {
            //2. 获取表名
            //tableName = Right_listBox.Items[0].ToString();

            //3. 获取“其他项”里面的信息

            //3.1 获取命名空间
            string ns = namespace_textBox.Text.Trim();  //命名空间

            //3.1.1 判断“命名空间是否不为空”
            if (string.IsNullOrEmpty(ns))
            {
                MessageBox.Show("请输入命名空间!");
                return;
            }

            //3.2 获取表前缀
            string tablePrefix = table_prefix_textBox.Text.Trim();
            //3.2.1 去掉前缀
            string className = tableName.Replace(tablePrefix, "");

            //3.2.2 将类名第一个字母进行大写
            className = className.Substring(0, 1).ToUpper() + className.Substring(1);

            //3.3 获取作者信息
            string author = author_textBox.Text.Trim();
            //4. 获取连接字符串
            string connStr = getConnstr();

            //5. 获取 Model 模板代码
            preview_textEditorControl.Text = ModelTemplate.GetModelTemplate(ns, tableName, author, className, connStr);
        }
示例#2
0
        /// <summary>
        /// 生成 Model文件
        /// </summary>
        /// <param name="ns"></param>
        /// <param name="author"></param>
        /// <param name="outputPath"></param>
        /// <param name="tablePrefix"></param>
        /// <param name="connStr"></param>
        public static void GenerateModel(string ns, string author, string outputPath, string tablePrefix, string connStr, ListBox.ObjectCollection items)
        {
            string modelOutputPath = "";

            if (outputPath.LastIndexOf("\\") == 0)
            {
                //如果生成路径的最后一位是“\”,是在该路径下的 Model 文件夹下面建立 Model 文件
                modelOutputPath = outputPath + "Model\\";
            }
            else //没有反斜杠,则要补上
            {
                modelOutputPath = outputPath + "\\Model\\";
            }

            //检查 Model 文件夹是否存在,不存在则创建
            if (!Directory.Exists(modelOutputPath))
            {
                Directory.CreateDirectory(modelOutputPath);
            }

            string className = "";

            //遍历要生成的表,生成 Model
            foreach (string tableName in items)
            {
                //去掉表名前缀
                className = tableName.Replace(tablePrefix, "");
                //将类名第一个字母大写
                className = className.Substring(0, 1).ToUpper() + className.Substring(1);
                //定义文件路径
                string filePath = modelOutputPath + className + ".cs";
                //声明文档流对象,采取覆盖模式
                StreamWriter sw = new StreamWriter(filePath, false);
                //生成文档
                sw.Write(ModelTemplate.GetModelTemplate(ns, tableName, author, className, connStr));
                //关闭文档流
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }
        }