Hey Fellow Coders,
I want to open a dialog to find out what the community does in this type of situation. I need to connect to the SQL Server I have for this Window. The code below works just fine, but I don't like it. What I really want is a way to "make a connection" without a connection string in the code. Either make a file (.txt, .properties, .settings, .whatever) and read the connection string from there, or create a single class and set the connection string (or strings) one time.
P.S. I don't want this:
using (
var conn =
new SqlConnection(@"Data Source=server_name;Initial Catalog=DB;Integrated Security=True;Persist Security Info=False;Encrypt=True;TrustServerCertificate=True;"))
I would rather use a resource, reference, class, whatever. I don't want to change this every time I release this code to someone new. I guess I want a "set it once and forget it" method.
P.S. Very Important - Releasing the code via ClickOnce in Visual Studio. Therefore, whatever method needs to work with ClickOnce.
public static void GetPatient()
{
try
{
using (
var conn =
new SqlConnection(@"Data Source=server_name;Initial Catalog=DB;Integrated Security=True;Persist Security Info=False;Encrypt=True;TrustServerCertificate=True;"))
{
var preQuery = Settings.Default.SqlGetPatient;
var queryString = preQuery + "@PatId = '" + PatId + "'";
conn.Open();
Log.Info("Established a SQL Connection and opened communication to database for patient: " + PatId);
using (var command = new
SqlCommand(queryString, conn))
{
var reader = command.ExecuteReader();
while (reader.Read())
{
PatName = reader[0] as string;
PatId = reader[1] as string;
PatSys = reader[2] as string;
PatAge = reader[3] as string;
break;
}
}
Log.Debug("Note Lookup as: " + queryString);
Log.Debug("DB Connection as:" + conn);
conn.Close();
}
}
catch (SqlException ex)
{
var msg = ex.ToString();
var title = Resources.CustomMsgBxTitle;
MessageBoxManager.Ok = "Upload";
MessageBoxManager.Cancel = "Ignore";
MessageBoxManager.Register();
var result = MessageBox.Show(msg, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
switch (result)
{
case DialogResult.OK:
Clipboard.SetText(msg);
Process.Start("http://weburl.com/open.php");
break;
case DialogResult.Cancel:
break;
}
MessageBoxManager.Unregister();
}
Log.Info("Passing values to MainWindow: Patient ID: " + PatId + ", Patient Name: " + PatName +
", Patient SYS_ID: " +
PatSys + ", Patient Age: " + PatAge + " - END");
}