private void PrepareDatabase() { MasterConnection.Open(); try { var timeout = MasterConnection.ConnectionTimeout; var userCount = MasterConnection.QuerySingle <int>(@" SELECT COUNT(*) USER_COUNT FROM SYS.DBA_USERS WHERE USERNAME = :UserName", new { UserName = _userName }, null, timeout); if (userCount > 0) { if (_dropUser) { MasterConnection.Execute($"DROP USER {_userName} CASCADE", null, null, timeout); } else { return; } } MasterConnection.Execute($@"CREATE USER {_userName} IDENTIFIED BY {_userPassword}", null, null, timeout); MasterConnection.Execute($@"GRANT DBA TO {_userName}", null, null, timeout); } finally { MasterConnection.Close(); } }
private void PrepareDatabase() { MasterConnection.Open(); try { var timeout = MasterConnection.ConnectionTimeout; var dbId = MasterConnection.QuerySingleOrDefault <long?>( "SELECT DB_ID(@DatabaseName) as DatabaseId", new { DatabaseName = _databaseName }, null, timeout); if (dbId.HasValue) { if (_dropDatabase) { MasterConnection.Execute("DROP DATABASE " + _databaseName, null, null, timeout); } else { return; } } MasterConnection.Execute("CREATE DATABASE " + _databaseName, null, null, timeout); } finally { MasterConnection.Close(); } }
public int Initialize(string jobName) { int rc = 0; Clear(); if (pState >= 0) // ONLY if we are using XDB! { // Check the Connection. if (MasterConnection != null) { if (MasterConnection.State != System.Data.ConnectionState.Open) { try { MasterConnection.Open(); } catch (SqlException ex) { rc = ex.ErrorCode; XLogger(2308, ex.ErrorCode, string.Format("JobName={0}; Message={1}", jobName, ex.Message)); } catch (Exception ex) { rc = -1; XLogger(2309, -1, string.Format("JobName={0}; Message={1}", jobName, ex.Message)); } } if (MasterConnection.State == System.Data.ConnectionState.Open) { try { myAdapter = new SqlDataAdapter(); // Try to get the Serial (if >= 0). string strTemp = "select * "; strTemp += " from WorkJobs a"; strTemp += " where a.Job_Name = '{0}'"; String strSelectCommand = String.Format(strTemp, jobName); myJobs.Clear(); myCommandBuilder = new SqlCommandBuilder(myAdapter); myAdapter.SelectCommand = new SqlCommand(strSelectCommand, MasterConnection); pState = myAdapter.Fill(myJobs); // add handlers myAdapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating); myAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand().Clone(); myAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both; myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand().Clone(); myAdapter.DeleteCommand = myCommandBuilder.GetDeleteCommand().Clone(); //myAdapter.InsertCommand.CommandText = String.Concat(myAdapter.InsertCommand.CommandText, // "; SELECT MyTableID=SCOPE_IDENTITY()"); string insertSuffix = "; SELECT Job_Serial=SCOPE_IDENTITY()"; myAdapter.InsertCommand.CommandText += insertSuffix; myAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; //SqlParameter[] aParams = new SqlParameter[myCommandBuilder.GetInsertCommand().Parameters.Count]; //myCommandBuilder.GetInsertCommand().Parameters.CopyTo(aParams, 0); //myCommandBuilder.GetInsertCommand().Parameters.Clear(); SqlParameter identParam = new SqlParameter("@id", SqlDbType.BigInt, 0, "Job_Serial"); identParam.Direction = ParameterDirection.Output; myAdapter.InsertCommand.Parameters.Add(identParam); string test = myAdapter.InsertCommand.Parameters["@id"].ToString(); myAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord; } catch (SqlException ex) { rc = ex.ErrorCode; XLogger(2310, ex.ErrorCode, string.Format("Serial={0}; Message={1}", jobName, ex.Message)); } catch (Exception ex) { rc = -1; XLogger(2311, -1, string.Format("Serial={0}; Message={1}", jobName, ex.Message)); } } else { rc = -1; pState = -1; XLogger(2312, 0, string.Format("Connection Unable to be Opened")); } } else { rc = -1; pState = -1; XLogger(2313, 0, string.Format("No Connection Available")); } try { theseJobs = myJobs.Select(); if (theseJobs.Length > 0) { // Clear Member Values! Clear(); jobSerial = (int)theseJobs[0]["Job_Serial"]; rc = jobSerial; jobHost = (string)theseJobs[0]["Job_Host"]; jobName = (string)theseJobs[0]["Job_Name"]; jobType = (int)theseJobs[0]["Job_Type"]; if (!DBNull.Value.Equals(theseJobs[0]["Job_Start"])) { jobStart = (string)theseJobs[0]["Job_Start"]; } if (!DBNull.Value.Equals(theseJobs[0]["Job_Stop"])) { jobStop = (string)theseJobs[0]["Job_Stop"]; } jobSequence = (int)theseJobs[0]["Job_Sequence"]; jobStatus = (int)theseJobs[0]["Job_Status"]; jobResult = (int)theseJobs[0]["Job_Result"]; jobPriority = (int)theseJobs[0]["Job_Priority"]; jobLimit = (int)theseJobs[0]["Job_Limit"]; if (!DBNull.Value.Equals(theseJobs[0]["Job_Display"])) { jobDisplay = (string)theseJobs[0]["Job_Display"]; } jobRetention = (int)theseJobs[0]["Job_Retention"]; jobEvent = (int)theseJobs[0]["Job_Event"]; changeUser = (string)theseJobs[0]["Change_User"]; changeDate = (string)theseJobs[0]["Change_Date"].ToString(); // NEED TO ADD THESE! //if (!DBNull.Value.Equals(theseComponents[0]["Change_Tag"])) //{ // changeTag = (string)theseComponents[0]["Change_Tag"]; //} //if (!DBNull.Value.Equals(theseComponents[0]["Change_State"])) //{ // changeState = (int)theseComponents[0]["Change_State"]; //} } } catch (SqlException ex) { rc = ex.ErrorCode; XLogger(2314, ex.ErrorCode, string.Format("JobName={0}; Message={1}", jobName, ex.Message)); } catch (Exception ex) { rc = -1; XLogger(2315, rc, string.Format("JobName={0}; Message={1}", jobName, ex.Message)); } } return(rc); }