private static void ConnectOrThrow( ABB.Vtrin.cDataLoader dataloader, string data_source, string db_username, string db_password) { // Set up a memory stream to catch exceptions using (MemoryStream memoryStream = new MemoryStream()) { TraceListener listener = new TextWriterTraceListener(memoryStream, "connectlistener"); Trace.Listeners.Add(listener); // Convert password to a secure string SecureString db_password_secure = new SecureString(); db_password.ToList().ForEach(c => db_password_secure.AppendChar(c)); // Initialize the database driver driver = dataloader.Connect( data_source, db_username, db_password_secure, ABB.Vtrin.cDataLoader.cConnectOptions.AcceptNewServerKeys | ABB.Vtrin.cDataLoader.cConnectOptions.AcceptServerKeyChanges, out _); // Unbind the connect listener Trace.Listeners.Remove("connectlistener"); // Case: driver is null, something went wrong // > throw an error if (driver == null) { // Read stack trace from the memorystream buffer string msg = Encoding.UTF8.GetString(memoryStream.GetBuffer()); throw new ApplicationException(msg); } } }
private static void ConnectOrThrow( ABB.Vtrin.cDataLoader dataloader, string rtdbHost, string rtdbUsername, string rtdbPassword) { // Set up a memory stream to catch exceptions using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { var listener = new System.Diagnostics.TextWriterTraceListener(memoryStream, "connectlistener"); System.Diagnostics.Trace.Listeners.Add(listener); // Set connection options dataloader.ConnectOptions = ABB.Vtrin.cDataLoader.cConnectOptions.AcceptNewServerKeys | ABB.Vtrin.cDataLoader.cConnectOptions.AcceptServerKeyChanges; // Initialize the database driver RTDBDriver = dataloader.Connect( rtdbHost, rtdbUsername, rtdbPassword, false); // Unbind the connect listener System.Diagnostics.Trace.Listeners.Remove("connectlistener"); // Case: driver is null, something went wrong // > throw an error if (RTDBDriver == null) { // Read stack trace from the memorystream buffer string msg = System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer()); throw new System.ApplicationException(msg); } } }