public void Execute(ConnectionParams cp, string commandText) { ClearAll(); DataSet dataSet = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(); try { using (SqlConnection conn = cp.CreateSqlConnection(true, false)) { cmd.Connection = conn; cmd.CommandTimeout = 0; cmd.CommandText = commandText; adapter.SelectCommand = cmd; adapter.Fill(dataSet); } } finally { if (cmd != null) { cmd.Dispose(); cmd = null; } if (adapter != null) { adapter.Dispose(); adapter = null; } } RenderDataSet(dataSet); }
private bool SyncTestConnection(bool showErrorDialog) { bool result = false; ConnectionParams cConn = GetCurrentConnectionSpec(); cConn.TimeOut = "15"; using (SqlConnection conn = cConn.CreateSqlConnection(false, false)) { try { conn.Open(); result = true; } catch (Exception ex) { if (showErrorDialog) { MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { throw ex; } } } return(result); }
public void LoadDataTypes(ConnectionParams cp, bool wantEmpty) { ConnectionParams tmpCp = cp; if (tmpCp == null) { tmpCp = _cp; } if (_tbl != null) { _tbl.PrimaryKey = null; _tbl = null; } cmbType.Items.Clear(); ClearAll(); string cmdText = ResManager.GetDBScript("Script_GetDataTypes"); using (SqlConnection conn = tmpCp.CreateSqlConnection(true, false)) { _tbl = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(cmdText, conn); adapter.Fill(_tbl); } DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = _tbl.Columns["name"]; _tbl.PrimaryKey = PrimaryKeyColumns; if (wantEmpty) { cmbType.Items.Add(String.Empty); } foreach (DataRow row in _tbl.Rows) { cmbType.Items.Add(((string)row["name"]).ToLowerInvariant()); } if (cmbType.Items.Count > 0) { cmbType.SelectedIndex = 0; } EvaluateDataType(); }
private void TryToConnect() { _cp.TimeOut = "15"; using (SqlConnection conn = _cp.CreateSqlConnection(false, false)) { try { conn.Open(); } catch (Exception ex) { if (!_cancelled) { throw ex; } } } }
private void ListObjects(string objType, string filter) { string commandText = String.Empty; if (objType == "All") { string inCr = PrepareInCriteria(); //ResManager.GetDBScript("Script_ObjectList_InCriteria"); commandText = String.Format(ResManager.GetDBScript("Script_ObjectListAll"), inCr, filter); } else { commandText = String.Format(ResManager.GetDBScript("Script_ObjectList"), objType, filter); } grdSource.DataSource = null; if (_dtSource != null) { _dtSource.Dispose(); _dtSource = null; } using (SqlConnection conn = _cp.CreateSqlConnection(true, false)) { SqlCommand cmd = new SqlCommand(commandText); cmd.Connection = conn; SqlDataAdapter ad = new SqlDataAdapter(); try { ad.SelectCommand = cmd; _dtSource = new DataTable(); ad.Fill(_dtSource); bsSource.DataSource = _dtSource; grdSource.DataSource = bsSource; } finally { ad.Dispose(); ad = null; } } }
public void LoadObjects(ConnectionParams cp, bool wantEmpty) { cmb.Items.Clear(); string inFilter = GenerateInFilter(); if (String.IsNullOrEmpty(inFilter)) { return; } ConnectionParams tmpCp = cp; if (tmpCp == null) { tmpCp = _cp; } string cmdText = "declare @cmplevel int select @cmplevel = cmptlevel from master..sysdatabases where name = DB_NAME() "; cmdText += "select so.id, so.name, so.xtype, CASE WHEN @cmplevel < 90 THEN USER_NAME(so.uid) ELSE SCHEMA_NAME(so.uid) END 'owner' from sysobjects so"; cmdText += " WHERE so.xtype in " + inFilter; cmdText += " order by CASE WHEN @cmplevel < 90 THEN USER_NAME(so.uid) ELSE SCHEMA_NAME(so.uid) END, so.name"; if (wantEmpty) { cmb.Items.Add(null); } using (SqlConnection conn = tmpCp.CreateSqlConnection(true, false)) { SqlDataReader reader = null; DbObjectSelectorItem item = null; SqlCommand cmd = new SqlCommand(cmdText, conn); reader = cmd.ExecuteReader(); try { while (reader.Read()) { item = new DbObjectSelectorItem(); item.ID = reader.GetInt32(0); item.Name = reader.GetString(1); item.ItemType = GetItemType(reader.GetString(2)); item.Owner = reader.GetString(3); cmb.Items.Add(item); } } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } } } if (cmb.Items.Count > 0) { cmb.SelectedIndex = 0; } }