Ejemplo n.º 1
0
        private void DoSearch(string DBName)
        {
            string ConnStr      = string.Empty;
            string ObjectName   = string.Empty;
            string SchemaName   = string.Empty;
            string DatabaseName = string.Empty;
            string ObjectType   = string.Empty;
            string MatchesOn    = string.Empty;
            string ObjectDetail = string.Empty;
            int    ObjectCount  = 0;
            string ExactMatch   = string.Empty;
            string TempType     = string.Empty;
            string ObjectId     = string.Empty;

            if (cExactMatch.Checked)
            {
                ExactMatch = "Y";
            }
            else
            {
                ExactMatch = "N";
            }

            GetServerDetail();

            ConnStr = ConnectionString + ";Database=" + DBName;

            try
            {
                lStatusRight.Text = "";
                lStatusLeft.Text  = "Searching " + DBName + "...";
                Application.DoEvents();

                if (CancelNow)
                {
                    return;
                }

                using (DataStuff sn = new DataStuff())
                {
                    DataSet ds = sn.Search(ConnStr, tSearchString.Text, ObjectsSelected, ExactMatch);

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            if (dtObjects == null)
                            {
                                dtObjects = ds.Tables[0];
                            }
                            else
                            {
                                dtObjects.Merge(ds.Tables[0]);
                            }

                            if (ds.Tables.Count > 1)
                            {
                                if (dtColumns == null)
                                {
                                    dtColumns = ds.Tables[1];
                                }
                                else
                                {
                                    dtColumns.Merge(ds.Tables[1]);
                                }
                            }

                            if (ds.Tables.Count > 2)
                            {
                                if (dtDetail == null)
                                {
                                    dtDetail = ds.Tables[2];
                                }
                                else
                                {
                                    dtDetail.Merge(ds.Tables[2]);
                                }
                            }

                            if (dtObjects.Rows.Count > 0)
                            {
                                foreach (DataRow row in dtObjects.Rows)
                                {
                                    Application.DoEvents();

                                    if (CancelNow)
                                    {
                                        return;
                                    }

                                    ObjectName   = row["ObjectName"].ToString();
                                    SchemaName   = row["SchemaName"].ToString();
                                    DatabaseName = row["DatabaseName"].ToString();
                                    ObjectType   = row["ObjectType"].ToString();
                                    ObjectId     = row["ObjectID"].ToString();
                                    MatchesOn    = "Name";

                                    //If the type is a table or view, get the detail columns

                                    switch (ObjectType)
                                    {
                                    case "SQL_STORED_PROCEDURE":
                                        ObjectType = "Stored Procedure";
                                        break;

                                    case "USER_TABLE":
                                        ObjectType = "User Table";
                                        break;

                                    case "VIEW":
                                        ObjectType = "View";
                                        break;

                                    case "SYSTEM_TABLE":
                                        ObjectType = "System Table";
                                        break;

                                    case "SQL_SCALAR_FUNCTION":
                                        ObjectType = "Function";
                                        break;

                                    case "CLR_SCALAR_FUNCTION":
                                        ObjectType = "Function";
                                        break;

                                    case "SQL_TABLE_VALUED_FUNCTION":
                                        ObjectType = "Function";
                                        break;

                                    case "AGGREGATE_FUNCTION":
                                        ObjectType = "Function";
                                        break;

                                    case "SQL_INLINE_TABLE_VALUED_FUNCTION":
                                        ObjectType = "Function";
                                        break;

                                    case "SQL_TRIGGER":
                                        ObjectType = "Trigger";
                                        break;

                                    case "DEFAULT_CONSTRAINT":
                                        ObjectType = "Constraint";
                                        break;

                                    case "FOREIGN_KEY_CONSTRAINT":
                                        ObjectType = "Constraint";
                                        break;

                                    case "PRIMARY_KEY_CONSTRAINT":
                                        ObjectType = "Constraint";
                                        break;

                                    case "UNIQUE_CONSTRAINT":
                                        ObjectType = "Constraint";
                                        break;

                                    default:
                                        break;
                                    }

                                    switch (ObjectType)
                                    {
                                    case "User Table":
                                        ObjectDetail = GetColumnDetail(ObjectName, ObjectType, ObjectId);

                                        if (ObjectName.ToUpper().Contains(tSearchString.Text.ToUpper()))
                                        {
                                            MatchesOn = "Name and Column";
                                        }
                                        else
                                        {
                                            MatchesOn = "Column";
                                        }

                                        break;

                                    case "View":
                                        ObjectDetail = GetColumnDetail(ObjectName, ObjectType, ObjectId);

                                        if (ObjectName.ToUpper().Contains(tSearchString.Text.ToUpper()))
                                        {
                                            MatchesOn = "Name and Column";
                                        }
                                        else
                                        {
                                            MatchesOn = "Column";
                                        }

                                        break;

                                    case "Stored Procedure":
                                        ObjectDetail = GetObjectDetail(ObjectName, ObjectType);

                                        if (ObjectName.ToUpper().Contains(tSearchString.Text.ToUpper()))
                                        {
                                            MatchesOn = "Name and Content";
                                        }
                                        else
                                        {
                                            MatchesOn = "Content";
                                        }

                                        break;

                                    case "Function":
                                        ObjectDetail = GetObjectDetail(ObjectName, ObjectType);

                                        if (ObjectName.ToUpper().Contains(tSearchString.Text.ToUpper()))
                                        {
                                            MatchesOn = "Name and Content";
                                        }
                                        else
                                        {
                                            MatchesOn = "Content";
                                        }

                                        break;

                                    case "Trigger":
                                        ObjectDetail = GetObjectDetail(ObjectName, ObjectType);

                                        if (ObjectName.ToUpper().Contains(tSearchString.Text.ToUpper()))
                                        {
                                            MatchesOn = "Name and Content";
                                        }
                                        else
                                        {
                                            MatchesOn = "Content";
                                        }

                                        break;

                                    case "Constraint":
                                        ObjectDetail = GetObjectDetail(ObjectName, ObjectType);

                                        MatchesOn = "Name";

                                        break;

                                    default:
                                        break;
                                    }

                                    if (ObjectType.Contains("Table"))
                                    {
                                        TempType = "Table";
                                    }
                                    else
                                    {
                                        TempType = ObjectType;
                                    }


                                    if (ObjectsSelected.Contains(TempType))
                                    {
                                        dgvObjects.Rows.Add(ObjectName, SchemaName, DatabaseName, ObjectType, MatchesOn, ObjectDetail, ObjectId);
                                        ObjectCount++;
                                    }
                                }
                            }

                            lStatusRight.Text = "Object count " + dgvObjects.Rows.Count.ToString();
                        }
                    }
                }

                if (dgvObjects.Rows.Count > 0)
                {
                    //lStatusLeft.Text = dgvObjects.Rows[0].Cells[3].Value.ToString() + " " + dgvObjects.Rows[0].Cells[0].Value.ToString();
                    lStatusLeft.Text = "Done";
                    dgvObjects.Focus();
                }
                else
                {
                    lStatusLeft.Text = "";
                }
            }

            catch
            {
            }
        }