Пример #1
0
        // model预览
        private void btnmodelyl_Click(object sender, EventArgs e)
        {
            if (lsbright.Items.Count == 0)
            {
                MessageBox.Show("请选择操作表.");
                return;
            }

            string tmp = cobdbtype.SelectedItem.ToString();

            string tabname = lsbright.Items[0].ToString();
            string ns      = txtnamespace.Text.Trim();

            if (string.IsNullOrEmpty(ns))
            {
                MessageBox.Show("请输入命名空间.");
                return;
            }
            string front     = txtfront.Text.Trim();
            string classname = tabname;

            if (front.Length > 0)
            {
                classname = tabname.Replace(front, "");
            }
            classname = classname.Substring(0, 1).ToUpper() + classname.Substring(1);
            string connstr = txtconnstr.Text.Trim();

            if (tmp == "Sqlite")
            {
                txtyl.Text = GenModel_SQLite.GenAllCode(ns, tabname, classname, connstr);
            }


            if (tmp == "MySQL")
            {
                txtyl.Text = GenModel_MySQL.GenAllCode(ns, tabname, classname, connstr);
            }

            if (tmp == "Access")
            {
                txtyl.Text = GenModel_Access.GenAllCode(ns, tabname, classname, connstr);
            }

            if (tmp == "SQL Server")
            {
                txtyl.Text = GenModel_MSSQL.GenAllCode(ns, tabname, classname, connstr);
            }
        }
Пример #2
0
        //生成代码
        private void btngen_Click(object sender, EventArgs e)
        {
            if (lsbright.Items.Count == 0)
            {
                MessageBox.Show("请选择操作表.");
                return;
            }

            string tmp = cobdbtype.SelectedItem.ToString();


            string output = txtoutput.Text.Trim();

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

            #region Model
            string output_model = "";
            if (output.Substring(output.Length - 2, 2) == "\\")
            {
                output_model = output + "Model\\";
            }
            else
            {
                output_model = output + "\\Model\\";
            }
            if (!Directory.Exists(output_model))
            {
                Directory.CreateDirectory(output_model);
            }

            string ns = txtnamespace.Text.Trim();
            if (string.IsNullOrEmpty(ns))
            {
                MessageBox.Show("请输入命名空间.");
                return;
            }
            string front     = txtfront.Text.Trim();
            string classname = "";
            string connstr   = txtconnstr.Text.Trim();

            foreach (string tabname in lsbright.Items)
            {
                classname = tabname;
                if (front.Length > 0)
                {
                    classname = tabname.Replace(front, "");
                }
                classname = classname.Substring(0, 1).ToUpper() + classname.Substring(1);

                string       filepath = output_model + classname + ".cs";
                StreamWriter sw       = new StreamWriter(filepath, false);
                if (tmp == "Sqlite")
                {
                    sw.Write(GenModel_SQLite.GenAllCode(ns, tabname, classname, connstr));
                }
                if (tmp == "MySQL")
                {
                    sw.Write(GenModel_MySQL.GenAllCode(ns, tabname, classname, connstr));
                }
                if (tmp == "SQL Server")
                {
                    sw.Write(GenModel_MSSQL.GenAllCode(ns, tabname, classname, connstr));
                }
                else if (tmp == "Access")
                {
                    sw.Write(GenModel_Access.GenAllCode(ns, tabname, classname, connstr));
                }
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }
            #endregion

            #region DAL
            string output_dal = "";
            if (output.Substring(output.Length - 2, 2) == "\\")
            {
                output_dal = output + "DAL\\";
            }
            else
            {
                output_dal = output + "\\DAL\\";
            }
            if (!Directory.Exists(output_dal))
            {
                Directory.CreateDirectory(output_dal);
            }

            foreach (string tabname in lsbright.Items)
            {
                classname = tabname;

                if (front.Length > 0)
                {
                    classname = tabname.Replace(front, "");
                }
                classname = classname.Substring(0, 1).ToUpper() + classname.Substring(1);

                string       filepath       = output_dal + classname + "DAL.cs";
                string       filepathExtend = output_dal + classname + "DALExtend.cs";
                StreamWriter sw             = new StreamWriter(filepath, false);
                StreamWriter swExtend       = new StreamWriter(filepathExtend, false);
                if (rad0.Checked)
                {
                    //基于微软企业库
                    sw.Write(GenDAL_MSSQL.GenAllCode(ns, tabname, classname, connstr));
                }
                else if (rad1.Checked)
                {
                    //基于UsTeam数据库操作类生成DAL
                    if (tmp == "Sqlite")
                    {
                        sw.Write(GenDAL_SQLite_niunan.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenSqliteExtend(ns, tabname, classname, connstr));
                    }
                    if (tmp == "MySQL")
                    {
                        sw.Write(GenDAL_MySQL_niunan.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenMysqlExtend(ns, tabname, classname, connstr));
                    }
                    if (tmp == "SQL Server")
                    {
                        sw.Write(GenDAL_MSSQL_niunan.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenMssqlExtend(ns, tabname, classname, connstr));
                    }
                    else if (tmp == "Access")
                    {
                        sw.Write(GenDAL_Access_niunan.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenAccessExtend(ns, tabname, classname, connstr));
                    }
                }
                else
                {
                    //基于Wcf数据库操作类生成DAL
                    if (tmp == "Sqlite")
                    {
                        sw.Write(GenDAL_SQLite_Wcf.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenSqliteExtend(ns, tabname, classname, connstr));
                    }
                    if (tmp == "MySQL")
                    {
                        sw.Write(GenDAL_MySQL_Wcf.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenMysqlExtend(ns, tabname, classname, connstr));
                    }
                    if (tmp == "Access")
                    {
                        sw.Write(GenDAL_Access_Wcf.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenAccessExtend(ns, tabname, classname, connstr));
                    }
                    if (tmp == "SQL Server")
                    {
                        sw.Write(GenDAL_MSSQL_Wcf.GenAllCode(ns, tabname, classname, connstr));
                        swExtend.Write(GenExtendClass.GenMssqlExtend(ns, tabname, classname, connstr));
                    }
                }


                sw.Flush();
                sw.Close();
                sw.Dispose();
                swExtend.Flush();
                swExtend.Close();
                swExtend.Dispose();
            }
            #endregion

            #region MSSQLHelper
            if (rad1.Checked)
            {
                if (tmp == "Sqlite")
                {
                    string       filepath = output_dal + "SQLiteHelper.cs";
                    StreamWriter sw       = new StreamWriter(filepath, false);
                    sw.Write(GenDAL_SQLite_niunan.GenSQLiteHelper(ns, connstr));
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
                if (tmp == "SQL Server")
                {
                    string       filepath = output_dal + "MSSQLHelper.cs";
                    StreamWriter sw       = new StreamWriter(filepath, false);
                    sw.Write(GenDAL_MSSQL_niunan.GenMSSQLHelper(ns, connstr));
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
                else if (tmp == "Access")
                {
                    string       filepath = output_dal + "AccessHelper.cs";
                    StreamWriter sw       = new StreamWriter(filepath, false);
                    sw.Write(GenDAL_Access_niunan.GenAccessHelper(ns, connstr));
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
                else if (tmp == "MySQL")
                {
                    string       filepath = output_dal + "MySQLHelper.cs";
                    StreamWriter sw       = new StreamWriter(filepath, false);
                    sw.Write(GenDAL_MySQL_niunan.GenMySQLHelper(ns, connstr));
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            #endregion

            MessageBox.Show("代码生成功.");
            txtyl.Text = "代码已生成到:" + output;
            System.Diagnostics.Process.Start(output);
        }