예제 #1
0
        private void btnLoadTables_Click(object sender, EventArgs e)
        {
            string databaseName      = txtDatabaseName.Text;
            string dataSource        = txtDataSource.Text;
            string initialCatalog    = txtInitialCatalog.Text;
            string userId            = txtUserId.Text;
            string password          = txtPassword.Text;
            bool   trustedConnection = chkTrustedConnection.Checked;
            string schema            = txtSchema.Text;

            try
            {
                SprocGenerator sprocGenerator = new SprocGenerator(databaseName,
                                                                   dataSource,
                                                                   initialCatalog,
                                                                   userId,
                                                                   password,
                                                                   trustedConnection,
                                                                   schema);
                _sprocGenerator = sprocGenerator;

                MetaInformationSchemaManager metaInformationSchemaManager =
                    new MetaInformationSchemaManager(databaseName,
                                                     dataSource,
                                                     initialCatalog,
                                                     userId,
                                                     password,
                                                     trustedConnection,
                                                     schema);

                foreach (MetaInformationSchema metaSchema in metaInformationSchemaManager.MetaDataList)
                {
                    //the reason we do this is because views blow up and are returned
                    //with this set from the query, so we just make sure we are looking
                    //only at base table type objects
                    if (metaSchema.MetaTable.TableType == CommonLibrary.Constants.SqlConstants.BASE_TABLE)
                    {
                        treeAvailableTables.Nodes.Add(metaSchema.MetaTable.TableName);
                    }
                }

                foreach (TreeNode node in treeAvailableTables.Nodes)
                {
                    node.Checked = true;
                }
            }

            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                MessageBox.Show(sqlEx.Message);
            }
            catch (Microsoft.SqlServer.Management.Smo.SmoException smo)
            {
                MessageBox.Show(smo.Message + smo.StackTrace);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #2
0
        private void btnGetCustomSprocs_Click(object sender, EventArgs e)
        {
            _customSprocs.Clear();
            treeCustomSprocs.Nodes.Clear();

            string databaseName      = txtDatabaseName.Text;
            string dataSource        = txtDataSource.Text;
            string initialCatalog    = txtInitialCatalog.Text;
            string userId            = txtUserId.Text;
            string password          = txtPassword.Text;
            bool   trustedConnection = chkTrustedConnection.Checked;
            string schema            = txtSchema.Text;

            _databaseSmoObjectsAndSettings = new CommonLibrary.DatabaseSmoObjectsAndSettings(databaseName,
                                                                                             dataSource,
                                                                                             initialCatalog,
                                                                                             userId,
                                                                                             password,
                                                                                             trustedConnection);

            _dataLayerGenerator = new DataLayerGenerator(_databaseSmoObjectsAndSettings,
                                                         this.GetType().Namespace);

            SprocDataLayerGenerator.PredicateFunctions predicateFunctions = new SprocDataLayerGenerator.PredicateFunctions();
            List <StoredProcedure> customSprocsNotGenerated = new List <StoredProcedure>();


            CommonLibrary.DatabaseSmoObjectsAndSettings databaseSmoObjectsAndSettings =
                new CommonLibrary.DatabaseSmoObjectsAndSettings(databaseName,
                                                                dataSource,
                                                                initialCatalog,
                                                                userId,
                                                                password,
                                                                trustedConnection,
                                                                schema);

            SprocGenerator sprocGenerator = new SprocGenerator(databaseName,
                                                               dataSource,
                                                               initialCatalog,
                                                               userId,
                                                               password,
                                                               trustedConnection,
                                                               schema);

            _sprocGenerator = sprocGenerator;
            List <StoredProcedure> sprocsGenerated = _sprocGenerator.GetStoredProcedures();

            if (sprocsGenerated != null)
            {
                foreach (StoredProcedure sproc in databaseSmoObjectsAndSettings.Database_Property.StoredProcedures)
                {
                    predicateFunctions.SprocNameHolder = sproc.Name;
                    if (!(sprocsGenerated.FindAll(predicateFunctions.FindSprocGeneratedBySprocName).Count > 0))
                    {
                        customSprocsNotGenerated.Add(sproc);
                    }
                }
            }

            if (customSprocsNotGenerated.Count > 0)
            {
                foreach (StoredProcedure customSproc in customSprocsNotGenerated)
                {
                    if (!customSproc.IsSystemObject)
                    {
                        treeCustomSprocs.Nodes.Add(customSproc.Name);
                        _customSprocs.Add(customSproc);
                    }
                }
            }
            btnGenerateCustomSprocDtos.Enabled = true;
            chkSelectAll.Enabled = true;
        }
예제 #3
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string databaseName      = txtDatabaseName.Text;
            string dataSource        = txtDataSource.Text;
            string initialCatalog    = txtInitialCatalog.Text;
            string userId            = txtUserId.Text;
            string password          = txtPassword.Text;
            bool   trustedConnection = chkTrustedConnection.Checked;
            string schema            = txtSchema.Text;

            try
            {
                SprocGenerator sprocGenerator = new SprocGenerator(databaseName,
                                                                   dataSource,
                                                                   initialCatalog,
                                                                   userId,
                                                                   password,
                                                                   trustedConnection,
                                                                   schema);
                _sprocGenerator = sprocGenerator;

                MetaInformationSchemaManager metaInformationSchemaManager =
                    new MetaInformationSchemaManager(databaseName,
                                                     dataSource,
                                                     initialCatalog,
                                                     userId,
                                                     password,
                                                     trustedConnection,
                                                     schema);

                SprocDataLayerGenerator.PredicateFunctions predicateFunctions =
                    new SprocDataLayerGenerator.PredicateFunctions();

                List <string> tablesToGen = new List <string>();
                List <MetaInformationSchema> schemasToGen = new List <MetaInformationSchema>();

                if (this.treeAvailableTables.Nodes.Count > 0)
                {
                    foreach (TreeNode node in treeAvailableTables.Nodes)
                    {
                        if (node.Checked)
                        {
                            tablesToGen.Add(node.Text);
                        }
                    }
                }

                if (tablesToGen.Count > 0)
                {
                    foreach (string tableName in tablesToGen)
                    {
                        predicateFunctions.TableNameHolder = tableName;
                        schemasToGen.AddRange(metaInformationSchemaManager.MetaDataList.FindAll(predicateFunctions.FindMetaInformationSchemaByTableName));
                    }
                }

                List <StoredProcedure> sprocsGenerated = sprocGenerator.GetStoredProcedures(schemasToGen);
                //StringBuilder sb = new StringBuilder();
                //foreach (StoredProcedure sproc in sprocsGenerated)
                //{
                //    sb.Append(sproc.TextHeader);
                //    sb.Append(sproc.TextBody);
                //    sb.Append(Environment.NewLine);
                //    sb.Append(Environment.NewLine);
                //    _sprocsGenerated.Add(sproc);
                //}
                //txtSprocsGenerated.Text = sb.ToString();

                Dictionary <StoredProcedure, string> sprocsThatCouldNotBeCreated;

                if (chkOverwriteExisting.Checked)
                {
                    sprocsThatCouldNotBeCreated = sprocGenerator.CreateNewStoredProceduresAndReplaceExistingOnes(sprocsGenerated);
                }
                else
                {
                    sprocsThatCouldNotBeCreated = sprocGenerator.CreateNewStoredProceduresDoNotReplaceExistingOnes(sprocsGenerated);
                }

                if (sprocsThatCouldNotBeCreated.Count > 0)
                {
                    StringBuilder errorBuilder = new StringBuilder();

                    errorBuilder.Append("Errors Encountered, could not create the following procedures:  ");
                    errorBuilder.Append(Environment.NewLine);

                    foreach (KeyValuePair <StoredProcedure, string> kvp in sprocsThatCouldNotBeCreated)
                    {
                        errorBuilder.Append("SprocName:  " + kvp.Key.Name + "ErrorMessage:  " + kvp.Value);
                        errorBuilder.Append(Environment.NewLine);
                        _sprocsThatCouldNotBeGenerated.Add(kvp.Key);
                    }
                    MessageBox.Show(errorBuilder.ToString());
                }
                else
                {
                    MessageBox.Show("Sproc Generation complete without errors");
                }
            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                MessageBox.Show(sqlEx.Message);
            }
            catch (Microsoft.SqlServer.Management.Smo.SmoException smo)
            {
                MessageBox.Show(smo.Message + smo.StackTrace);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            btnGetCustomSprocs.Enabled = true;
        }