Ejemplo n.º 1
0
        public void Add()
        {
            int count = 0;
            if (cboxDAL.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\DAL"))
                {
                    Directory.CreateDirectory(path + @"\DAL");
                }
            }
            if (cboxBLL.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\BLL"))
                {
                    Directory.CreateDirectory(path + @"\BLL");
                }
            }
            if (cboxModel.Checked)
            {
                count++;
                if (!Directory.Exists(path + @"\Models"))
                {
                    Directory.CreateDirectory(path + @"\Models");
                }
            }
            progressBar1.Value = 0;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = listSelectdTables.Count;

            foreach (string tablename in listSelectdTables)
            {
                list = new List<ProcedureInfo>();
                cn.Open();
                cn.ChangeDatabase(cboxDBSource.Text);
                SqlCommand cm = new SqlCommand(ProcedureInfo.GetColumnInfoSql(tablename), 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.ColumnName = (string)dr[0];
                        pro.DataType = (string)dr[1];
                        pro.Length = Convert.ToInt32(dr[2]);
                        pro.IsNull = Convert.ToBoolean(dr[3]);
                        pro.IsIdentity = Convert.ToBoolean(dr[4]);
                        pro.IsPK = Convert.ToBoolean(dr[5]);
                        pro.Description = Convert.ToString(dr[6]);
                        list.Add(pro);
                    }
                }
                if (cboxDAL.Checked)
                {
                    BuilderDAL.WriteDAL(path, txtProjectName.Text.Trim(), table, list);
                    BuilderDAL.WriteBaseDAL(path, txtProjectName.Text.Trim(),list);
                    BuilderDAL.WriteBuildWhereSQLServer(path, txtProjectName.Text.Trim());
                }
                if (cboxBLL.Checked)
                {

                    BuilderBLL.WriteBLL(path, table, list, txtProjectName.Text.Trim(), txtProjectName.Text.Trim(), txtProjectName.Text.Trim());
                }

                if (cboxModel.Checked)
                {
                    BuilderModel.WriteModel(path, table, list, txtProjectName.Text.Trim());
                }
                progressBar1.Value++;
            }
            this.progressBar1.Value = 0;
            MessageBox.Show("生成成功!", "提示");
            btnCancle.Visible = false;
        }
Ejemplo n.º 2
0
        private void CreateSQL()
        {
            string SQLstring = "";
            this.progressBar1.Maximum = listSelectdTables.Count;
            int count = 0;
            foreach (string tablename in listSelectdTables)
            {
                count++;
                this.progressBar1.Value = count;
                string sql = ProcedureInfo.GetColumnInfoSql(tablename);
                List<ProcedureInfo> list = new List<ProcedureInfo>();

                cn.Open();
                cn.ChangeDatabase(cboxDBSource.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.ColumnName = (string)dr[0];
                        pro.DataType = (string)dr[1];
                        pro.Length = Convert.ToInt32(dr[2]);
                        pro.IsNull = Convert.ToBoolean(dr[3]);
                        pro.IsIdentity = Convert.ToBoolean(dr[4]);
                        pro.IsPK = Convert.ToBoolean(dr[5]);
                        pro.Description = Convert.ToString(dr[6]);
                        list.Add(pro);
                    }
                }
                SQLstring += "go\r\n";
                cn.Open();
                cn.ChangeDatabase(cboxDBSource.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 (MyHelper.findModelsType(pro.DataType) == "string" || MyHelper.findModelsType(pro.DataType) == "DateTime")
                            {
                                SQLstring += "'";
                                SQLstring += dr[i].ToString();
                                SQLstring += "'";
                            }
                            else if (MyHelper.findModelsType(pro.DataType) == "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" + "\\" + cboxDBSource.Text + "SQL.txt");
            writer = file.CreateText();
            writer.WriteLine(SQLstring);
            writer.Close();

            MessageBox.Show("生成成功!", "提示");
            this.progressBar1.Value = 0;
            btnCancle.Visible = false;
        }