|
I have an app that I'm workin on on 3 different computers. Each has it's own connection string. It's a pain to try to get the rigt conn string plugged in. What's the right way to deal with this?
Everything makes sense in someone's mind
|
|
|
|
|
Look at your project in VS, and open "Properties" in the Solution Explorer (It should be just above "Resources").
Double click on "Settings.Settings", and create a string called "ConnectionString".
In your app:
Properties.Settings.Default.Reload();
string connString = Properties.Settings.Default.ConnectionString; To write a changed one, use:
Properties.Settings.Default.ConnectionString = myNewConnectionString;
Properties.Settings.Default.Save(); This can be different for each PC, and can be set as part of the setup installation - it is in an XML file in the App .EXE directory.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Ya but what about from a development standpoint? I would like my app to "know" what PC I'm on and just find the right conn string.
Everything makes sense in someone's mind
|
|
|
|
|
What makes it the "right" connection string?
Either there is something which identifies the PC and says "My connection string is 'xxx'" or you have to create a string on the fly. Or store a different string on each PC, either via the settings or via the registry - settings is easier and more modern.
There are various things you can read on the PC to identify it as "this pc", or "this user", but what says "this connection string"?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
This does:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Office"
connectionString="Data Source=(local);Initial Catalog=TDC;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="Home"
connectionString="Data Source=(local);Initial Catalog=TDC;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="Clients_PC"
connectionString="Data Source=(local);Initial Catalog=TDC;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
What I was considering doing was poping up an simple UI with a list of the 3 key names. Then IO can pick one and it's used for the duration of that session. When I'm done with the app, disable the UI and use the Client's connstring.
Everything makes sense in someone's mind
|
|
|
|
|
Provided you have a property, or method, that returns the connection string you could have something like:
private string ConnectionString
{
get
{
#IF DEBUG
if(System.Environment.MachineName == ???)
return Configuration.ConnectionString["Home"];
...
#ELSE
return Configuration.ConnectionString["Client_PC"];
#END
}
}
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Taking a closer look your connection strings they are all the same so why do you need to switch anything?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I write my own configuration files.
|
|
|
|
|
please i need this information for a work school.
|
|
|
|
|
Not sure what you mean - you question is not very clear.
If you mean "how do I execute a method automatically at some point in the future?" then look at a Timer:
Timer t = new Timer();
t.Interval = 10000;
t.Tick += new EventHandler(t_Tick);
t.Start();
...
void t_Tick(object sender, EventArgs e)
{
MyMethod(myParameters);
}
Would activate MyMethod every ten seconds.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Have an app that runs forever, and have it use either Thread.Sleep() or a timer to execute the same operation repeatedly.
Or have an app that does it once, and use Windows Scheduler to schedule its invocation.
|
|
|
|
|
Luc Pattyn wrote: Or have an app that does it once, and use Windows Scheduler to schedule its invocation.
At least the teacher would be amazed .
Greetings - Jacek
|
|
|
|
|
Hi,
msaez wrote: i need this information for a work school
Your question doesn't contain much information. An example of what it is that you're looking for is always helpful.
There's multiple ways to interpret the question, and even when taken at it's most literal, there'd be still multiple possible solutions. If it's a fixed set of methods, I'd recommend to encapsulate each in it's own executable. You could then use the Windows Task Scheduler to plan the activation of these executables. That would mean that half of your solution would depend on a well-tested part of Windows, and the other half would consist of executables. Modular, maintenance-friendly, can easily be extended.
The question would be whether that passes the requirements set by the teacher - he might be expecting something fancy that loads an assembly on a backgroundthread and which uses reflection to execute a specific method, complete with a dynamic inputscreen for the parameters
I are Troll
|
|
|
|
|
basically when i click the X (the one next to min, max),
want to call a function. But where's the entry point for this ?
wow
|
|
|
|
|
If it's Windows Forms, then there is a FormClosing event.
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
}
}
Greetings - Jacek
|
|
|
|
|
Well technically he wanted to know when the X was clicked, which doesn't map directly to FormClosing, since other events could cause the form to close.
If you specifically need to check that the X was clicked, checking the CloseReason on the FormClosingEventArgs might get you closer. Alternatively you can detect the mouse click event instead and figure out where you are. Can't think of a better way off the top of my head.
modified on Wednesday, November 17, 2010 9:27 AM
|
|
|
|
|
Hi All,
I have been searching for a solution for this for some time now but basically I want to copy a table from one SQL Instance to Another using ADO.Net while preserving the schema (including identity and primary key column) - do not want to use SMO
All I have managed to find is how to copy the contents or how to achieve with a single query (which is not possible as they are two different servers) - perhaps my searching skills are failing me on this day
Thanks for any suggestions
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
As I have had no luck with this one then I have decided to use an SQL script file to create the database tables before doing a bulk copy on the data that I need.
The SQL Script file has been created from SQL Management Studio.
I would prefer to do without the need for a script file so if anybody wants to contribute then please do
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
You can create the create table script as a string in C# and execute it over ADO but you still need a script somewhere.
You can also user Select INTO but this does not create the keys and constraints, they need to be scripted.
Your requirements are a little unrealistic not to want to use a script to do something as fundamental as creating an object in the database
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Have you searched the built-in stored procedures?
No guarantees, but I vaguely remember seeing one that does this.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi!
According to the Nested Type Usage Guidelines[^], creating an instance of a nested class by an outside code is generally bad.
I'd like to implement a factory+template+command pattern, in which creating an instance of a command would be forbidden. The factory has instances of all templates. Templates are responsible to validate user's input and, if it's valid, create a Command object. Command class executes itself and has a private constructor. Fortunately, a nested class can access a private constructor of it's parent.
I cannot use a static method, because the factory has to be extensible (it has an AddTemplate(CommandTemplate) method which allows adding new templates).
Here is the code:
class LookCommand : Command
{
public class Template : CommandTemplate
{
private const string NAME = "look";
public override Command TryCreate(RpgOntologyController ontc, CommandParser input)
{
return MatchesCommand(ontc, NAME, input)
? new LookCommand(ontc)
: null;
}
protected string CommandName
{
get { return NAME; }
}
}
private LookCommand(RpgOntologyController controller) : base(controller)
{
}
public override CommandEffect Execute()
{
StringBuilder description = new StringBuilder();
description.AppendLine(new SceneOntologyController(Controller).CurrentSceneDescription);
description.AppendLine();
return new CommandEffect
{
Output = description.ToString()
};
}
void AppendNPC(StringBuilder build)
{
foreach (string npc in new NPCOntologyController(Controller).NearbyNPCs)
{
build.AppendLine(npc);
}
}
}
In the factory:
public Command Create(string input)
{
Command cmd = null;
var commandParser = new CommandParser(input);
if (_templates.Any(template => (cmd = template.TryCreate(_controller, commandParser)) != null))
{
return cmd;
}
throw new ArgumentException("Invalid command.");
}
Can it be an exception of a rule from the Nested Type Usage Guidelines[^]?
Thanks in advance,
Greetings - Jacek
|
|
|
|
|
Yes, it is bad form to instantiate a nested class from outside of its parent. I can't think of a good exception to that rule.
The real question is why does it have to be nested??
|
|
|
|
|
As I have written, it has to be nested because only nested classes have access to a private constructor of another class (in this case is Command ).
Greetings - Jacek
|
|
|
|
|
Hi,
In my application, I use FTP do download single .txt file from multiple devices (up to 100 devices, each with its own IP_adress). At the moment I use single bacground worker, which contains single for loop and goes from first to last device and downloads file to local computer (size of each file up to 5 MB).
Is it better to implement separate backgroudworker for each device, and download files simultaneously and what would be the best approach?
|
|
|
|
|