Sandeep
Sandeep

Reputation: 5771

Checking the resource parameter in a C# method

I guess this would look a bit stupid, but I'm trying to find an easy way of doing this.

I have a bunch of SQL scripts in different files, which I have added as text file resources in my .net project. I pass each of these resource strings to an ExecuteScript method which gets the script executed in the database, with the help of a predefined connection string. It goes like this:

ExecuteScript(Resources.Script1);
ExecuteScript(Resources.Script2);
ExecuteScript(Resources.Script3);

private void ExecuteScript(string script)
{
    connectionString = // Get connection string from config file
    // Rest of the code to execute the script.
}

Now the problem arises when I would want to use different connection strings for different scripts. For example: I would like to use a connectionString1 for executing Resources.Script1, connectionString2 for Resources.Script2.

How do I do this in my ExecuteScript method itself? Is there a way to find the resource's name after it enters the method? Or should I define separate connection strings explicitly?

Upvotes: 0

Views: 956

Answers (2)

Peter Stock
Peter Stock

Reputation: 311

You can't achieve this unless the name of the resource or the connectionString is contained within the script.
I would go for something like this:

ExecuteScript(Resources.Script1, Resources.ConnectionString1);

Or, as alternative:

private void ExecuteScript(int index)
{
    var connectionString = Resources.ResourceManager.GetString(string.Format("ConnectionString{0}",index));
    var script = Resources.ResourceManager.GetString(string.Format("Script{0}",index));
// Rest of the code to execute the script.
}

Upvotes: 0

CodeTherapist
CodeTherapist

Reputation: 2806

You could use a more complex type as parameter.

ExecuteScript(Resources.Script1); 
ExecuteScript(Resources.Script2); 
ExecuteScript(Resources.Script3);  

private void ExecuteScript(ScriptContext scriptContrxt) 
{     
    connectionString = // Get connection string from config file     
    // Rest of the code to execute the script. 
}

Define a new class ScriptContext with two properties like this:

public class ScriptContext
{
     // may be create a constructor and make the setter private.
     public string script { get; set; }
     public ConnectionString ConnectionString { get; set; }
}

Than you can use for every script another connection string.

Upvotes: 2

Related Questions