Created a "login" dialog form to grab the server name from the user. Now a valid connection must be made before the Main form loads.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Windows.Forms;
|
||||
using AdvertsingProfitControl.Properties;
|
||||
|
||||
namespace AdvertsingProfitControl
|
||||
{
|
||||
public partial class FrmDatabaseSelector : Form
|
||||
{
|
||||
public bool _connectionSuccessful = false;
|
||||
|
||||
public FrmDatabaseSelector()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
var connectionString = "Data Source=" + serverNameTextBox.Text + ";" + "Initial Catalog=AdvertisingProfitControl;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework";
|
||||
try
|
||||
{
|
||||
//Test the connection string with a forced timeout of 2 seconds.
|
||||
using (var conn = new SqlConnection(connectionString + "Connection Timeout=2"))
|
||||
{
|
||||
conn.Open(); // throws if invalid
|
||||
}
|
||||
Settings.Default.ConnectionString = connectionString;
|
||||
_connectionSuccessful = true;
|
||||
this.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
label1.Text = ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user