public void RunSQLScript() { if (m_serverExplorer != null) { SQLSecuritySettings sss = new SQLSecuritySettings(m_serverExplorer); DialogResult dr = sss.ShowDialog(this); if (dr == DialogResult.OK) { string connectionstr = string.Format(SQLConnections._secureConnect, sss.SelectedDBName, sss.SelectedSQLServer); if (sss.SecurityMode() == SecurityType.Mixed) { connectionstr = string.Format(SQLConnections._secureConnect, sss.SelectedDBName, sss.SelectedSQLServer, sss.User, sss.PWD); } using (SqlConnection s = new SqlConnection(connectionstr)) { int rows = 0; try { s.InfoMessage += new SqlInfoMessageEventHandler(s_InfoMessage); s.Open(); using (SqlCommand sc = s.CreateCommand()) { // C01: LLEWIS: add test for GO statements to break up executequery statements string[] statements = GetText().ToLower().Split(new string[] { "go" }, StringSplitOptions.RemoveEmptyEntries); foreach (string statement in statements) { sc.CommandText = statement; try { sc.Prepare(); } catch { } // NOP, don't care about errors from this rows = sc.ExecuteNonQuery(); } } // dispose of SqlCommand: sc s.Close(); s.InfoMessage -= new SqlInfoMessageEventHandler(s_InfoMessage); MessageBox.Show("The SQL command was successful.", "SQL Info!", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (SqlException sqe) { s.Close(); string error = string.Format(SQLSchemaTool.ERRORFORMAT, sqe.Message, sqe.Source, sqe.StackTrace); logger.Warn(SQLSchemaTool.ERRORFORMAT, sqe.Message, sqe.Source, sqe.StackTrace); HTMLDoc hDoc = new HTMLDoc("SQL Error Report"); hDoc.HTMLText = error; hDoc.Show(this.DockPanel); MessageBox.Show("There was an error executing the SQL command:\n" + error, "SQL ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } else if (dr == DialogResult.Abort) { logger.Warn("You must set the server security first!"); MessageBox.Show("You must set the server security first!\nUse the SQL Server Explorer.", "SQL ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public void RunSQLScript() { if (m_serverExplorer != null) { SQLSecuritySettings sss = new SQLSecuritySettings(m_serverExplorer); DialogResult dr = sss.ShowDialog(this); if (dr == DialogResult.OK) { string connectionstr = string.Format(SQLConnections._secureConnect, sss.SelectedDBName, sss.SelectedSQLServer); if (sss.SecurityMode() == SecurityType.Mixed) { connectionstr = string.Format(SQLConnections._secureConnect, sss.SelectedDBName, sss.SelectedSQLServer, sss.User, sss.PWD); } using (SqlConnection s = new SqlConnection(connectionstr)) { int rows = 0; try { s.InfoMessage += new SqlInfoMessageEventHandler(s_InfoMessage); s.Open(); using (SqlCommand sc = s.CreateCommand()) { // C01: LLEWIS: add test for GO statements to break up executequery statements string[] statements = GetText().ToLower().Split(new string[]{"go"}, StringSplitOptions.RemoveEmptyEntries); foreach (string statement in statements) { sc.CommandText = statement; try { sc.Prepare(); } catch { } // NOP, don't care about errors from this rows = sc.ExecuteNonQuery(); } } // dispose of SqlCommand: sc s.Close(); s.InfoMessage -= new SqlInfoMessageEventHandler(s_InfoMessage); MessageBox.Show("The SQL command was successful.", "SQL Info!", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (SqlException sqe) { s.Close(); string error = string.Format(SQLSchemaTool.ERRORFORMAT, sqe.Message, sqe.Source, sqe.StackTrace); logger.Warn(SQLSchemaTool.ERRORFORMAT, sqe.Message, sqe.Source, sqe.StackTrace); HTMLDoc hDoc = new HTMLDoc("SQL Error Report"); hDoc.HTMLText = error; hDoc.Show(this.DockPanel); MessageBox.Show("There was an error executing the SQL command:\n" + error, "SQL ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } else if (dr == DialogResult.Abort) { logger.Warn("You must set the server security first!"); MessageBox.Show("You must set the server security first!\nUse the SQL Server Explorer.", "SQL ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public void ReHydrateDTS(string[] servers) { if (IsDTSPackage) { string PackageName = null; string Logfile = null; string PackageDescription = null; string _UID = null; string _PWD = null; string _SQLServer = null; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(txtEditCtl.Text); XmlNodeList xNodeList = xmlDoc.SelectNodes("/DTS_File/Package/Name"); PackageName = xNodeList.Count > 0 ? ((XmlNode)xNodeList.Item(0)).InnerText : ""; xNodeList = xmlDoc.SelectNodes("/DTS_File/Package/Description"); PackageDescription = xNodeList.Count > 0 ? ((XmlNode)xNodeList.Item(0)).InnerText : ""; xNodeList = xmlDoc.SelectNodes("/DTS_File/Package/LogFileName"); Logfile = xNodeList.Count > 0 ? ((XmlNode)xNodeList.Item(0)).InnerText : ""; xNodeList = null; xmlDoc = null; // create new DTS package object in memory DTSPackage2 oPackage = new DTSPackage2(PackageName, Logfile, PackageDescription); SQLSecuritySettings sss = new SQLSecuritySettings(servers); if (servers != null && servers.Length > 0) { sss.SetServerName(servers[0]); } DialogResult dr = sss.ShowDialog(this); if (dr == DialogResult.OK) { try { _UID = sss.User; _PWD = sss.PWD; _SQLServer = sss.SelectedSQLServer; // set up the DTS package authentication type if (sss.SecurityMode() == Lewis.SST.SQLMethods.SecurityType.Mixed) { oPackage.Authentication = DTSPackage2.authTypeFlags.Default; } else { oPackage.Authentication = DTSPackage2.authTypeFlags.Trusted; } // TODO: make this an async operation for larger dts files Cursor.Current = Cursors.WaitCursor; oPackage.Load(FileName); oPackage.Save(_SQLServer, _UID, _PWD, null); } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.Message); } finally { oPackage = null; Cursor.Current = Cursors.Default; } } } }