-2

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?

Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • 6
    DataSource => Data Source with a space. – Coskun Ozogul Mar 07 '18 at 13:59
  • 3
    Closing as a simple typo of no use for future readers – Steve Mar 07 '18 at 14:01
  • 1
    Possible duplicate of [Keyword not supported: "data source" initializing Entity Framework Context](https://stackoverflow.com/questions/6997035/keyword-not-supported-data-source-initializing-entity-framework-context) – rahulaga-msft Mar 07 '18 at 14:01
  • @RahulAgarwal No, that one actually has the space but has a different issue. – juharr Mar 07 '18 at 14:03
  • 1
    I disagree with both the down votes and the closing of this question as "On Hold". The problem is not a typo, it is using the wrong keyword. The question is well written and included example code. There is a clear error message that is searchable. Yes, the answer seems is easy to someone who is familiar with connection strings, but for those who are not, this question may help them find an answer in the future. – Paul Williams Mar 07 '18 at 15:16
  • 1
    @PaulWilliams Please explain how it is not a typo to write `DataSource` instead of `Data Source`. If `DataSource` *was* a keyword, I could understand that, but that option doesn't exist – Camilo Terevinto Mar 07 '18 at 15:33
  • 1
    @CamiloTerevinto You and I may know that the keyword is "Data Source", and that it is not equivalent to "DataSource". Someone who is not familiar with connection strings may not. If you follow the link in my answer, you will see there are many equivalent keywords: "Data Source", "Server", "Address", "Addr", and "Network Address". The asker may have seen these keywords in other code and assumed that "DataSource" is also acceptable. On the other hand, mis-typing the server name would be a typo specific to their environment, and the resolution would not be very useful in future searches. – Paul Williams Mar 07 '18 at 16:05
  • 1
    @CamiloTerevinto Note that this is an easy mistake to make. I have coded "DataSource" instead of "Data Source" before in a connection string. This question may help someone like me resolve the problem in the future. – Paul Williams Mar 07 '18 at 16:26
  • @PaulWilliams I agree. It totally didn't occur to me that it could be a typo. I've read on SO to try various alternatives, such as changing the keyword to Server... it didn't cross my mind to check for empty space. – Prometheus Mar 07 '18 at 16:41
  • I tried to delete my answer that was marked as accepted as @PaulWilliams has provided a more detailed answer. Please mark it as the answer as it contains the same information as my answer did. – Ross Bush Mar 07 '18 at 18:47
  • @RossBush just did. I accepted your answer back than as you responded first. Thank you both for the prompt assistance. – Prometheus Mar 07 '18 at 20:36

2 Answers2

1

You are missing a space in your connection string.

Change:

DataSource={0}

To:

Data Source={0}
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
1

There should be a space between "Data" and "Source" in your connection string. The keyword is "Data Source" and not "DataSource":

string connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", Variables.setDb1ServerName, Variables.setDb1Name, Variables.setDb1User, Variables.setDb2Password);

Reference: see the list of ConnectionString keywords for use with the SqlConnection class.

Paul Williams
  • 16,585
  • 5
  • 47
  • 82