private void DoSearch(string srchValue, bool incCols) { this.UseWaitCursor = true; this.lstSearchResults.BeginUpdate(); this.lstSearchResults.Items.Clear(); this.lstSearchResults.ShowGroups = true; try { rsDbSql dbConn = this.GetDbObject(this._sqlExplorer.SelectedNode); bool noResults = true; foreach (string tbl in dbConn.SearchForTable(srchValue, true)) { this.lstSearchResults.Items.Add(new ListViewItem(tbl, 2, this.lstSearchResults.Groups["grpTables"])); noResults = false; } foreach (string view in dbConn.SearchForView(srchValue, true)) { this.lstSearchResults.Items.Add(new ListViewItem(view, 8, this.lstSearchResults.Groups["grpViews"])); noResults = false; } foreach (string sp in dbConn.SearchForStoredProceedure(srchValue, true)) { this.lstSearchResults.Items.Add(new ListViewItem(sp, 4, this.lstSearchResults.Groups["grpStoredProcs"])); noResults = false; } foreach (string func in dbConn.SearchForFunction(srchValue, true)) { this.lstSearchResults.Items.Add(new ListViewItem(func, 4, this.lstSearchResults.Groups["grpFuncs"])); noResults = false; } try { if (incCols) { try { foreach (string dbNm in dbConn.GetDatabaseList()) { try { foreach (string tbl in dbConn.GetTableList(dbNm)) { try { foreach (ColumnParams cp in dbConn.GetColumns(dbNm, tbl)) { try { if (cp.ColumnName.Contains(srchValue)) { this.lstSearchResults.Items.Add(new ListViewItem(string.Format("{0}.{1}.{2} ({3})", dbNm, tbl, cp.ColumnName, cp.DataType.ToString()), 5, this.lstSearchResults.Groups["grpCols"])); noResults = false; } } catch (Exception ex) { if (ex.InnerException is System.Data.SqlClient.SqlException && ex.ToString().Contains("The server principal ") && ex.ToString().Contains(" is not able to access the database ") && ex.ToString().Contains(" under the current security context.")) { // We're going to ignore this error and continue // searching whichever databases we *are* // allowed to access. } else { throw; } } } } catch (Exception ex) { if (ex.InnerException is System.Data.SqlClient.SqlException && ex.ToString().Contains("The server principal ") && ex.ToString().Contains(" is not able to access the database ") && ex.ToString().Contains(" under the current security context.")) { // We're going to ignore this error and continue // searching whichever databases we *are* // allowed to access. } else { throw; } } } } catch (Exception ex) { if (ex.InnerException is System.Data.SqlClient.SqlException && ex.ToString().Contains("The server principal ") && ex.ToString().Contains(" is not able to access the database ") && ex.ToString().Contains(" under the current security context.")) { // We're going to ignore this error and continue // searching whichever databases we *are* // allowed to access. } else { throw; } } } } catch (Exception ex) { if (ex.InnerException is System.Data.SqlClient.SqlException && ex.ToString().Contains("The server principal ") && ex.ToString().Contains(" is not able to access the database ") && ex.ToString().Contains(" under the current security context.")) { // We're going to ignore this error and continue // searching whichever databases we *are* // allowed to access. } else { throw; } } } } catch (Exception ex) { MessageBox.Show(this, "Unexpected Error", "An error occured while trying to search for the specified column:\n" + ex.Message); } // If nothing got added, we'll just add an entry informing the user // that no objects matching their search were found. if (noResults) { this.lstSearchResults.ShowGroups = false; this.lstSearchResults.Items.Add(new ListViewItem("No matches found...")); } this.splitContainer1.Panel2Collapsed = false; this.txtSearch.Text = string.Empty; } catch (Exception ex) { #if DEBUG MessageBox.Show(this, "An error occured while trying to perform the search:\n\n" + ex.ToString() + "\n\nApplication Version: " + Application.ProductVersion, "Error"); #else MessageBox.Show(this, "An error occured while trying to perform the search:\n\n" + ex.Message + "\n\nApplication Version: " + Application.ProductVersion, "Error"); #endif } finally { this.lstSearchResults.EndUpdate(); this.UseWaitCursor = false; } }
private void RefreshDbList(TreeNode nd) { nd.Text += " (expanding...)"; Application.DoEvents(); this.BeginUpdate(); this.UseWaitCursor = true; try { rsDbSql dbConn = (rsDbSql)nd.Tag; if (dbConn == null) { throw new ArgumentException("Selected TreeNode does not reference a connection string."); } nd.Nodes.Clear(); foreach (string db in dbConn.GetDatabaseList()) { TreeNode ndDatabase = new TreeNode(); ndDatabase.Text = db; ndDatabase.Name = db; ndDatabase.Tag = SqlObjType.Database; ndDatabase.ImageKey = ndDatabase.SelectedImageKey = "Database"; ndDatabase.ContextMenuStrip = this.mnuNdDb; TreeNode ndTables = new TreeNode("Tables"); ndTables.Name = "grpTables"; ndTables.Nodes.Add("<EMPTY>"); ndTables.Tag = SqlObjType.TableGroup; ndTables.ContextMenuStrip = this.mnuNdGroup; ndDatabase.Nodes.Add(ndTables); TreeNode ndViews = new TreeNode("Views"); ndViews.Name = "grpViews"; ndViews.Nodes.Add("<EMPTY>"); ndViews.Tag = SqlObjType.ViewGroup; ndViews.ContextMenuStrip = this.mnuNdGroup; ndDatabase.Nodes.Add(ndViews); TreeNode ndStoredProcs = new TreeNode("Stored Proceedures"); ndStoredProcs.Name = "grpStoredProcs"; ndStoredProcs.Nodes.Add("<EMPTY>"); ndStoredProcs.Tag = SqlObjType.StoredProcGroup; ndStoredProcs.ContextMenuStrip = this.mnuNdGroup; ndDatabase.Nodes.Add(ndStoredProcs); TreeNode ndFuncsT = new TreeNode("Table-valued Functions"); ndFuncsT.Name = "grpTFuncs"; ndFuncsT.Nodes.Add("<EMPTY>"); ndFuncsT.Tag = SqlObjType.FunctionGroup; ndFuncsT.ContextMenuStrip = this.mnuNdGroup; TreeNode ndFuncsS = new TreeNode("Scalar-valued Functions"); ndFuncsS.Name = "grpSFuncs"; ndFuncsS.Nodes.Add("<EMPTY>"); ndFuncsS.Tag = SqlObjType.FunctionGroup; ndFuncsS.ContextMenuStrip = this.mnuNdGroup; TreeNode ndFuncs = new TreeNode("Functions"); ndFuncs.Nodes.Add(ndFuncsT); ndFuncs.Nodes.Add(ndFuncsS); ndDatabase.Nodes.Add(ndFuncs); TreeNode ndUsers = new TreeNode("Users"); ndUsers.Nodes.Add("<EMPTY>"); ndUsers.Tag = SqlObjType.UserGroup; ndUsers.ContextMenuStrip = this.mnuNdGroup; ndDatabase.Nodes.Add(ndUsers); nd.Nodes.Add(ndDatabase); } } catch (Exception ex) { MessageBox.Show(this, string.Format("Unable to retreive the list of databases from the SQL server:\n\n{0}\n\nApplication Version: {1}", ex.Message, Application.ProductVersion), "Error"); } finally { nd.Text = nd.Text.Substring(0, nd.Text.LastIndexOf('(')).TrimEnd(); this.EndUpdate(); this.UseWaitCursor = false; } }