I want test if a connection to an SQL server (on local corporate intranet) is available. I want to test the connection with data that's stored in Application Settings.
My attempt is below.
First, I wrote the following helper class:
public class SqlHelper
{
SqlConnection cn;
public SqlHelper(string connectionString)
{
cn = new SqlConnection(connectionString);
}
public bool isConnection
{
get
{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
return true;
}
}
Than, I created the following variables:
public class Variables
{
public static string setDb1ServerName = WindowsFormsApp3.Properties.Settings.Default.Db1ServerName;
public static string setDb1Name = WindowsFormsApp3.Properties.Settings.Default.Db1Name;
public static string setDb1User = WindowsFormsApp3.Properties.Settings.Default.Db1User;
public static string setDb2Password = WindowsFormsApp3.Properties.Settings.Default.Db2Password;
}
And finally, I created a button to test the connection:
private void txtConnection1_Click(object sender, EventArgs e)
{
string connectionString = string.Format("DataSource={0};Initial Catalog={1};User ID={2};Password={3};", Variables.setDb1ServerName, Variables.setDb1Name, Variables.setDb1User, Variables.setDb2Password);
try
{
SqlHelper helper = new SqlHelper(connectionString);
if (helper.isConnection)
MessageBox.Show("Конекцијата е успешна", "Порака", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Порака", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The error i'm getting when I click the button is:
Keyword not supported: 'datasource'
Anyone?