Beispiel #1
0
        public void Add()
        {
            int count = 0;
            if (checkBox2.Checked)
            {
                count++;
            }
            if (checkBox3.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\DAL"))
                {
                    Directory.CreateDirectory(path + @"\DAL");
                }
            }
            if (checkBox4.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\BLL"))
                {
                    Directory.CreateDirectory(path + @"\BLL");
                }
            }
            if (checkBox5.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\Models"))
                {
                    Directory.CreateDirectory(path + @"\Models");
                }
            }
            progressBar1.Value = 0;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = li.Count;
            inserString = "\r\nuse " + database + "\r\ngo\r\n"; ;
            dalString = "";
            bllString = "";
            foreach (string tablename in li)
            {

                string sql = "SELECT col.name AS 列名, typ.name as 数据类型,col.max_length AS 占用字节数," +
                       "  col.is_nullable  AS 是否允许非空,col.is_identity  AS 是否自增," +
                       " case when exists  ( SELECT 1  FROM sys.indexes idx join sys.index_columns idxCol on (idx.object_id = idxCol.object_id)" +
                       "  WHERE idx.object_id = col.object_id AND idxCol.index_column_id = col.column_id AND idx.is_primary_key = 1" +
                       " ) THEN 1 ELSE 0 END  AS 是否是主键 FROM sys.columns col left join sys.types typ on (col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id)" +
                       " WHERE col.object_id =(SELECT object_id FROM sys.tables WHERE name = '" + tablename + "')" +
                       "select t.name as 表名,c.Name as 列名,type1.name as 类型,c.length as 长度,col.is_nullable  AS 是否允许非空,col.is_identity  AS 是否自增 from sysColumns c,sys.tables t,sys.types type1,sys.columns col where t.name='singertab' and t.object_id = c.id and c.xusertype=type1.user_type_id and  col.object_id =(SELECT object_id FROM sys.tables WHERE name = '" + tablename + "')";

                list = new List<ProcedureInfo>();

                cn.Open();
                cn.ChangeDatabase(comboBox1.Text);
                SqlCommand cm = new SqlCommand(sql, cn);
                table = tablename.Substring(0, 1).ToUpper() + tablename.Substring(1);
                using (SqlDataReader dr = cm.ExecuteReader(CommandBehavior.CloseConnection))
                {

                    while (dr.Read())
                    {
                        ProcedureInfo pro = new ProcedureInfo();
                        pro.Row = (string)dr[0];
                        pro.Type = (string)dr[1];
                        pro.Length = Convert.ToInt16(dr[2]);
                        pro.IsNull = Convert.ToBoolean(dr[3]);
                        pro.IsIdentity = Convert.ToBoolean(dr[4]);
                        list.Add(pro);
                    }
                }
                if (checkBox2.Checked)
                {

                    if (myMethod.MyAdd1)
                    {
                        SQLAdd();
                    }
                    if (myMethod.MyAddReturnId1)
                    {
                        SQLAddReturnId();
                    }
                    if (myMethod.MyChange1)
                    {
                        SQLChange();
                    }
                    if (myMethod.MyDelete1)
                    {
                        SQLDelete();
                    }
                    if (myMethod.MySelectAll1)
                    {
                        SQLSelectAll();
                    }
                    if (myMethod.MySelectById1)
                    {
                        SQLSelectById();
                    }
                    if (myMethod.MySelectByWhere1)
                    {
                        SQLSelectByWhere();
                    }
                }
                if (checkBox3.Checked)
                {

                    dalString = "";
                    dalString += "using System;\r\n";
                    dalString += "using System.Collections.Generic;\r\n";
                    dalString += "using " + textBox9.Text.Trim() + ";\r\n";
                    dalString += "using " + textBox7.Text.Trim() + ";\r\n";
                    dalString += "using System.Data;\r\n";
                    dalString += "using System.Data.SqlClient;\r\n";
                    dalString += "\r\n";
                    dalString += "namespace " + textBox5.Text.Trim() + "\r\n";
                    dalString += "{\r\n";
                    dalString += "    public class " + table + "Service\r\n";
                    dalString += "    {\r\n";

                    if (myMethod.MyAdd1)
                    {
                        DALAdd();
                    }
                    if (myMethod.MyAddReturnId1)
                    {
                        DALAddReturnId();
                    }
                    if (myMethod.MyChange1)
                    {
                        DALChange();
                    }
                    if (myMethod.MyDelete1)
                    {
                        DALDelete();
                    }
                    if (myMethod.MySelectAll1)
                    {
                        DALSelectAll();
                    }
                    if (myMethod.MySelectById1)
                    {
                        DALSelectById();
                    }
                    if (myMethod.MySelectByWhere1)
                    {
                        DALSelectByWhere();
                    }
                    file = new FileInfo(path + @"\DAL" + "\\" + table + "Service.cs");
                    writer = file.CreateText();
                    writer.WriteLine(dalString);
                    writer.Close();
                }
                if (checkBox4.Checked)
                {

                    bllString = "";
                    bllString += "using System;\r\n";
                    bllString += "using System.Collections.Generic;\r\n";
                    bllString += "using " + textBox5.Text.Trim() + ";\r\n";
                    bllString += "using " + textBox9.Text.Trim() + ";\r\n";
                    bllString += "\r\n";
                    bllString += "namespace " + textBox8.Text.Trim() + "\r\n";
                    bllString += "{\r\n   public class " + table + "Manager\r\n";
                    bllString += "   {\r\n";
                    bllString += "        \r\n       ";
                    bllString += fristToUpper(table) + "Service dal = new "+fristToUpper(table) + "Service();\r\n\r\n";

                    if (myMethod.MyAdd1)
                    {
                        BLLAdd();
                    }
                    if (myMethod.MyAddReturnId1)
                    {
                        BLLAddReturnId();
                    }
                    if (myMethod.MyChange1)
                    {
                        BLLChange();
                    }
                    if (myMethod.MyDelete1)
                    {
                        BLLDelete();
                    }
                    if (myMethod.MySelectAll1)
                    {
                        BLLSelectAll();
                    }
                    if (myMethod.MySelectById1)
                    {
                        BLLSelectById();
                    }
                    if (myMethod.MySelectByWhere1)
                    {
                        BLLSelectByWhere();
                    }

                    file = new FileInfo(path + @"\BLL\" + table + "Manager.cs");
                    writer = file.CreateText();
                    writer.WriteLine(bllString);
                    writer.Close();

                }

                if (checkBox5.Checked)
                {

                    StreamWriter write = new StreamWriter(path + @"\Models\" + table + ".cs", false, Encoding.Default);
                    StringBuilder sb = new StringBuilder();
                    sb.Append("using System;");
                    sb.Append(Environment.NewLine);
                    sb.Append("using System.Collections.Generic;");
                    sb.Append(Environment.NewLine);
                    sb.Append("using System.Text;");
                    sb.Append(Environment.NewLine);
                    sb.Append(Environment.NewLine);
                    sb.Append("namespace ");
                    sb.Append(textBox9.Text);
                    sb.Append(Environment.NewLine);
                    sb.Append("{");
                    sb.Append(Environment.NewLine);
                    sb.Append("    [Serializable]");
                    sb.Append(Environment.NewLine);
                    sb.Append("    public class ");
                    sb.Append(table);
                    sb.Append(Environment.NewLine);
                    sb.Append(createplace(4));
                    sb.Append("{");
                    sb.Append(Environment.NewLine);

                    foreach (ProcedureInfo pro in list)
                    {
                        string name = pro.Row;
                        string type = pro.Type;

                        sb.Append(createplace(8));
                        sb.Append("private");
                        sb.Append(" ");
                        sb.Append(findModelsType(type));
                        sb.Append(createplace(1));
                        sb.Append(name.Substring(0, 1).ToLower() + name.Substring(1));
                        sb.Append(";");
                        sb.Append(Environment.NewLine);
                        sb.Append(Environment.NewLine);
                        sb.Append(createplace(8));
                        sb.Append("public");
                        sb.Append(" ");
                        sb.Append(findModelsType(type));
                        sb.Append(" ");
                        sb.Append(name.Substring(0, 1).ToUpper() + name.Substring(1));
                        sb.Append(Environment.NewLine);
                        sb.Append(createplace(8));
                        sb.Append("{");
                        sb.Append(Environment.NewLine);
                        sb.Append(createplace(10));
                        sb.Append("get { return ");
                        sb.Append(name.Substring(0, 1).ToLower() + name.Substring(1));
                        sb.Append(";}");
                        sb.Append(Environment.NewLine);
                        sb.Append(createplace(10));
                        sb.Append("set { ");
                        sb.Append(name.Substring(0, 1).ToLower() + name.Substring(1));
                        sb.Append("=value");
                        sb.Append(";}");
                        sb.Append(Environment.NewLine);
                        sb.Append(createplace(8));
                        sb.Append("}");
                        sb.Append(Environment.NewLine);

                    }
                    sb.Append(createplace(4));
                    sb.Append("}");
                    sb.Append(Environment.NewLine);
                    sb.Append("}");

                    write.WriteLine(sb.ToString());
                    write.Flush();
                    write.Close();
                    write = null;
                }
                progressBar1.Value++;
            }

            if (!Directory.Exists(path + @"\Procedure"))
            {
                Directory.CreateDirectory(path + @"\Procedure");
            }
            file = new FileInfo(path + @"\Procedure" + "\\" + comboBox1.Text + ".txt");
            writer = file.CreateText();
            writer.WriteLine(inserString);
            writer.Close();
        }
Beispiel #2
0
        private void button5_Click(object sender, EventArgs e)
        {
            this.chenggong.Visible = false;
            this.timer1.Enabled = false;
            string SQLstring = "";
            this.progressBar1.Maximum = li.Count;
            int count = 0;
            foreach (string tablename in li)
            {
                count++;
                this.progressBar1.Value = count;
                string sql = "SELECT col.name AS 列名, typ.name as 数据类型,col.max_length AS 占用字节数," +
                       "  col.is_nullable  AS 是否允许非空,col.is_identity  AS 是否自增," +
                       " case when exists  ( SELECT 1  FROM sys.indexes idx join sys.index_columns idxCol on (idx.object_id = idxCol.object_id)" +
                       "  WHERE idx.object_id = col.object_id AND idxCol.index_column_id = col.column_id AND idx.is_primary_key = 1" +
                       " ) THEN 1 ELSE 0 END  AS 是否是主键 FROM sys.columns col left join sys.types typ on (col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id)" +
                       " WHERE col.object_id =(SELECT object_id FROM sys.tables WHERE name = '" + tablename + "')" +
                       "select t.name as 表名,c.Name as 列名,type1.name as 类型,c.length as 长度,col.is_nullable  AS 是否允许非空,col.is_identity  AS 是否自增 from sysColumns c,sys.tables t,sys.types type1,sys.columns col where t.name='singertab' and t.object_id = c.id and c.xusertype=type1.user_type_id and  col.object_id =(SELECT object_id FROM sys.tables WHERE name = '" + tablename + "')";
                List<ProcedureInfo> list = new List<ProcedureInfo>();

                cn.Open();
                cn.ChangeDatabase(comboBox1.Text);
                SqlCommand cm = new SqlCommand(sql, cn);
                string table = tablename.Substring(0, 1).ToUpper() + tablename.Substring(1);
                using (SqlDataReader dr = cm.ExecuteReader(CommandBehavior.CloseConnection))
                {

                    while (dr.Read())
                    {
                        ProcedureInfo pro = new ProcedureInfo();
                        pro.Row = (string)dr[0];
                        pro.Type = (string)dr[1];
                        pro.Length = Convert.ToInt16(dr[2]);
                        pro.IsNull = Convert.ToBoolean(dr[3]);
                        pro.IsIdentity = Convert.ToBoolean(dr[4]);
                        list.Add(pro);
                    }
                }
                SQLstring += "go\r\n";
                cn.Open();
                cn.ChangeDatabase(comboBox1.Text);
                 sql = "select * from " + table;
                 cm = new SqlCommand(sql, cn);
                using (SqlDataReader dr = cm.ExecuteReader(CommandBehavior.CloseConnection))
                {

                    while (dr.Read())
                    {
                        SQLstring += "insert into " + table + " values(";
                        int i = 0;
                        foreach (ProcedureInfo pro in list)
                        {
                            if (pro.IsIdentity)
                            {
                                i++;
                                continue;
                            }
                            if (DBNull.Value == dr[i])
                            {
                                SQLstring += "null";
                            }
                            else if (findModelsType(pro.Type) == "string" || findModelsType(pro.Type) == "DateTime")
                            {
                                SQLstring += "'";
                                SQLstring += dr[i].ToString();
                                SQLstring += "'";
                            }
                            else if (findModelsType(pro.Type)=="bool")
                            {
                                if(Convert.ToBoolean(dr[i]))
                                    SQLstring += 1.ToString();
                                else
                                    SQLstring += 0.ToString();
                            }
                            else
                            {
                                SQLstring += dr[i].ToString();
                            }
                            if (i < list.Count-1)
                            {
                                SQLstring += " ,";
                            }
                            i++;
                        }
                        SQLstring += ")";
                        SQLstring += "\r\n";

                    }
                }
            }
            if (!Directory.Exists(path + @"\SQL"))
            {
                Directory.CreateDirectory(path + @"\SQL");
            }
            file = new FileInfo(path + @"\SQL" + "\\" + comboBox1.Text + "SQL.txt");
            writer = file.CreateText();
            writer.WriteLine(SQLstring);
            writer.Close();
            this.chenggong.Visible = true;
            this.timer1.Enabled = true;
        }