public static Lpp.Dns.DataMart.Model.PCORIQueryBuilder.DataContext CreatePCORIDataContext(Model.Settings.SQLProvider dbProvider, string connectionString, bool logToConsole = true, string schema = null) { string defaultSchema = ""; System.Data.Common.DbConnection connection; if (dbProvider == Settings.SQLProvider.SQLServer) { connection = new System.Data.SqlClient.SqlConnection(connectionString); } else if (dbProvider == Settings.SQLProvider.ODBC) { connection = new System.Data.Odbc.OdbcConnection(connectionString); } else if (dbProvider == Settings.SQLProvider.PostgreSQL) { defaultSchema = schema ?? "dbo"; connection = new Npgsql.NpgsqlConnection(connectionString); } else if (dbProvider == Settings.SQLProvider.Oracle) { defaultSchema = schema ?? "C##PCORNETUSER"; connection = new Oracle.ManagedDataAccess.Client.OracleConnection(connectionString); } else { throw new NotSupportedException("The specified SQLProvider of " + dbProvider + " is not supported."); } var db = new Lpp.Dns.DataMart.Model.PCORIQueryBuilder.DataContext(connection, defaultSchema); if (logToConsole) { db.Database.Log = (s) => { Console.WriteLine(s); }; } return(db); }
private void btnTestConnection_Click(object sender, EventArgs e) { var selectedProvider = (Model.Settings.SQLProvider)cmbDataProvider.SelectedItem; bool isOracle = selectedProvider == Model.Settings.SQLProvider.Oracle; string port = txtPort.Text.Trim(); int number; if (!string.IsNullOrWhiteSpace(txtPort.Text.Trim()) && !int.TryParse(port, out number) && !isOracle) { MessageBox.Show("Please Enter Valid Port Number"); return; } int connectionTimeout; if (!int.TryParse(txtConnectionTimeout.Value.ToString(), out connectionTimeout)) { MessageBox.Show("Please Enter Valid Connection Timeout Value in seconds."); return; } int commandTimeout; if (!int.TryParse(txtCommandTimeout.Value.ToString(), out commandTimeout)) { MessageBox.Show("Please Enter Valid Command Timeout Value in seconds."); return; } string server = txtServer.Text; if (string.IsNullOrWhiteSpace(txtServer.Text.Trim()) && isOracle) { MessageBox.Show("Please Enter Valid Server."); return; } string database = txtDatabase.Text; if (string.IsNullOrWhiteSpace(txtDatabase.Text.Trim()) && isOracle) { MessageBox.Show("Please Enter Valid Oracle SID."); return; } string userID = txtUserID.Text; string password = txtPassword.Text; if (string.IsNullOrWhiteSpace(userID) && !string.IsNullOrWhiteSpace(password)) { MessageBox.Show("Please Enter Valid User ID."); return; } if (!string.IsNullOrWhiteSpace(userID) && string.IsNullOrWhiteSpace(password) && isOracle && (userID != "/")) { MessageBox.Show("Please Enter Valid Password."); return; } string dsn = (cmbDataSourceName.SelectedItem ?? string.Empty).ToString(); Model.Settings.SQLProvider sqlProvider = (Model.Settings.SQLProvider)Enum.Parse(typeof(Model.Settings.SQLProvider), cmbDataProvider.SelectedItem.ToString(), true); Dictionary <string, object> settings = new Dictionary <string, object>(); settings.Add("DataProvider", cmbDataProvider.SelectedItem.ToString()); settings.Add("Server", server); settings.Add("Port", port); settings.Add("Database", database); settings.Add("UserID", userID); settings.Add("Password", password); settings.Add("ConnectionTimeout", connectionTimeout); settings.Add("CommandTimeout", commandTimeout); try { var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies(); Assembly assembly = (from a in currentAssemblies where a.GetName().Name == "Lpp.Dns.DataMart.Client.Database" select a).FirstOrDefault(); if (assembly == null) { assembly = Assembly.LoadFrom("Lpp.Dns.DataMart.Client.Database.dll"); } Type dbTesterType = assembly.GetType("Lpp.Dns.DataMart.Client.Database.ConnectionTester"); MethodInfo methodInfo = dbTesterType.GetMethod("Test"); var classInstance = Activator.CreateInstance(dbTesterType, null); object[] parameters = new[] { settings }; var result = methodInfo.Invoke(classInstance, parameters); if (Convert.ToBoolean(result)) { MessageBox.Show("Connection Successful", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Unable to connect, please verify settings.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }catch (Exception ex) { MessageBox.Show(ex.Message, "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }