1

I've a databaseconnectionString variable in file databasetablename.cs and I want to use this databaseconnectionString in my other files without creating the connection string details for each api,following is how my project structure looks like and how I tried to use the variable databaseconnectionString which throws an error doesnot exist in current context ,what am I missing?how to use the databaseconnectionString in product.cs file?

Repository
   --->BuildRepositoryfolder
       Product.cs
   --->DatabaseConnections
       databasetablename.cs


databasetablename.cs

    public class databasetablename
    {
        public string databaseconnectionString = "server=xx.xx.xxx.xxx;database=aci_dev;uid=wciadmin;pwd=adSHEEP91min;";
    }

Product.cs
      public IEnumerable<Info> InfoSearch(LookaheadRunsFilterCriteria filterCriteria)
       {  
            var conn = new MySql.Data.MySqlClient.MySqlConnection();
            conn.ConnectionString = databaseconnectionString; --> throws an error

       }
Jeff Short
  • 285
  • 1
  • 5
  • 14
  • 1
    The convention is to put connection strings in your app.config or web.config file. Is this an ASP.NET project? WPF? UWP? WinForm? – Dour High Arch Mar 23 '17 at 23:54
  • ya,this is ASP.net..what exactly should I put?can you provide an example? – Jeff Short Mar 24 '17 at 00:02
  • Look at [Setting up connection string in ASP.NET](http://stackoverflow.com/questions/5642474/). The same code works on databases other than SQL Server. – Dour High Arch Mar 24 '17 at 00:06

3 Answers3

1

In C#, to use a variable defined in one class from another there are several requirements. If DatabaseTable is a class with a variable ConnectionString that you want to use inside another class Program, then:

  1. The two classes must be in the same assembly/project and ConnectionString must be marked public or internal -- OR -- ConnectionString must be marked public and DatabaseTable must be marked public and the project with Program must reference the project with DatabaseTable.

In one project:

class Program {
    public static void Main() {
        var str = DatabaseTable.ConnectionString;
    }
}

class DatabaseTable {
    internal static string ConnectionString = "test";
}

In separate projects:

class Program {
    public static void Main() {
        var str = DatabaseTable.ConnectionString;
    }
}

public class DatabaseTable {
    public static string ConnectionString = "test";
}

Access modifiers (MSDN)


  1. If you want ConnectionString to be available to Program by just referencing the DatabaseTable class, without creating an instance of that class, then ConnectionString must be marked static or const.

With instances:

class Program {
    public static void Main() {
        var table = new DatabaseTable();
        var str = table.ConnectionString;
    }
}

public class DatabaseTable {
    public string ConnectionString = "test";
}

With static:

class Program {
    public static void Main() {
        var str = DatabaseTable.ConnectionString;
    }
}

public class DatabaseTable {
    public static string ConnectionString = "test";
}

const (MSDN)

static (MSDN)

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
0

I suppose that's what you want:

conn.ConnectionString = databasetable.databaseconnectionString;
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

You may also try singleton pattern. I will give You example I have in my project already. I am using OleDbConnection, but You may adjust it for Your purpose ( MySqlConnection ). You will have to connect only once. Your methods related to database could be also inside Broker.cs . Later, for example inside form, when needed, You will be using Broker.dajBrokera().methodName(). You will have to add using namespace (name) too. Note private constructor, and dajBrokera() which will create only one instance of Broker class.

public class Broker
{
  OleDbConnection konekcija;

  OleDbCommand komanda;

  void konektujSe()
  {
        konekcija = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\...;Persist Security Info=False;");
        komanda = konekcija.CreateCommand();
  }

  private static Broker broker;

  public static Broker dajBrokera()
  {
      if (broker == null)
      {
          broker = new Broker();
      }
      return broker;
  }

  private Broker()
  {
      konektujSe();
  }
}

Hope this can help. Sorry for using OleDbConnection, instead MySqlConnection.It would take me more time to answer, and I suppose faster answer could be more helpful. Best regards.

mike_cus
  • 67
  • 1
  • 10