From 213e7b297ee1aa58472a6cbc0f4fe01a054686a8 Mon Sep 17 00:00:00 2001 From: Kathy Date: Fri, 10 Jan 2020 15:34:59 +0000 Subject: [PATCH] =?UTF-8?q?C2017-003:=20Support=20SQL=20Server=20=E2=80=93?= =?UTF-8?q?=20don=E2=80=99t=20allow=20input=20of=20connection=20string=20i?= =?UTF-8?q?f=20migration=20command=20line=20argument=20is=20not=20used=20o?= =?UTF-8?q?r=20if=20user=20is=20not=20admin.=20Also=20check=20for=20valid?= =?UTF-8?q?=20connection=20before=20save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frmRODbProperties.cs | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/PROMS/VEPROMS User Interface/frmRODbProperties.cs b/PROMS/VEPROMS User Interface/frmRODbProperties.cs index aeae059e..81b27130 100644 --- a/PROMS/VEPROMS User Interface/frmRODbProperties.cs +++ b/PROMS/VEPROMS User Interface/frmRODbProperties.cs @@ -9,6 +9,7 @@ using System.Windows.Forms; using System.IO; using VEPROMS.CSLA.Library; using DevComponents.DotNetBar; +using Volian.Base.Library; namespace VEPROMS { @@ -70,9 +71,25 @@ namespace VEPROMS _origFolderPath = (_roDbInfo == null) ? null : _roDbInfo.FolderPath; _origSQLConnect = (_roDbInfo == null) ? null : (_roDbInfo.DBConnectionString == null || _roDbInfo.DBConnectionString == "" || _roDbInfo.DBConnectionString == "cstring") ? null : _roDbInfo.DBConnectionString; // Disable the OK button when initialized. Enable it if the user makes changes + ppBtnTestSQL.Visible = ppBtnTestSQL.Enabled = CanMigrateRoAccessToSql(roDbInfo); + ppTxtSQL.Visible = ppTxtSQL.Enabled = CanMigrateRoAccessToSql(roDbInfo); + ppLblSQL.Visible = CanMigrateRoAccessToSql(roDbInfo); ppBtnOk.Enabled = false; } + private bool CanMigrateRoAccessToSql(RODbInfo rODbi) + { + // C2017-003: This method is used to determine whether the sql server version can be used & if there is data. + // A command line argument 'RoInSql'. For now, this argument must be used to allow code to run for ro->sql. Later + // this will be changed so that if argument is used, the program does NOT include test button & text box to allow + // entry of sql server connection string + if (!Volian.Base.Library.VlnSettings.GetCommandFlag("RoInSql")) return false; + // The following conditions must be true in order to migrate the set the connection string. + // 1) the user must be an admin + UserInfo ui = UserInfo.GetByUserID(VlnSettings.UserID); + if (!ui.IsAdministrator()) return false; + return true; + } private void ppBtnFldrDlg_Click(object sender, EventArgs e) { FolderBrowserDialog dlgROFolder = new FolderBrowserDialog(); @@ -108,7 +125,25 @@ namespace VEPROMS { // C2017-003: ro in sql. 'cstring' in connection string represents using MS Access database // if connect string is null, set connect string to "cstring" otherwise, use what user typed in: - roDb.DBConnectionString = (ppTxtSQL.Text == null || ppTxtSQL.Text == "") ? "cstring" : ppTxtSQL.Text; + // first check if connection can be made to what was entered: + bool canconnect = false; + try + { + using (SqlConnection connection = new SqlConnection(ppTxtSQL.Text)) + { + try + { + connection.Open(); + canconnect = (connection.State == ConnectionState.Open); + } + catch (SqlException) { } + } + } + catch (Exception ex) {} + if (canconnect) + roDb.DBConnectionString = (ppTxtSQL.Text == null || ppTxtSQL.Text == "") ? "cstring" : ppTxtSQL.Text; + else + MessageBox.Show("SQL Connection failed, connection string won't be saved.", "Cannot save connection data"); } if (_origFolderPath != ppTxtPath.Text) {