简体   繁体   中英

how do I use a connecting string variable defined in a file reuse in another file in same C# project?

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

       }

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)

我想这就是你想要的:

conn.ConnectionString = databasetable.databaseconnectionString;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM