Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

i have a winform and some querry to save data.
i created à class(Connexion.cs) to put the connexion string.
in this class , i have to connexion:
1-DataBase For Test
2-Database for Prod

i added in the winform a combobox(value :Prod,Test).
when the user select Prod the connexion must be "Prod"
when the user select Testthe connexion must be "Test"

this is the connexion string in Connexion.cs

<pre lang="C#">
using System;

using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Avancement
{
    class Connexion
    {
 //*************************************************//

public static SqlConnection cnx = new SqlConnection("Data Source = ***\\DB; Initial Catalog = <big>BD_Name</big>; Persist Security Info = True; User ID = ***; Password =***");
}



i want to pass the information to this class
the variable remplace BD_Name
so <when the="" user="" select="" prod="" connexion="" must="" be="" "prod"

public="" static="" sqlconnection="" cnx="new" sqlconnection("data="" source="***\\DB;" initial="" catalog="<big">Prod; Persist Security Info = True; User ID = ***; Password =***");

when the user select Testthe connexion must be "Test"


public static SqlConnection cnx = new SqlConnection("Data Source = ***\\DB; Initial Catalog = Test; Persist Security Info = True; User ID = ***; Password =***");

What I have tried:

i tried to pass variable but i can find the code
i created a public string in winform("BD") to set "Prod " or "Test"
then i tried to get it from class but nothing (form1.BD)
Posted
Updated 27-Oct-21 23:10pm
Comments
BillWoodruff 28-Oct-21 8:16am    
please format all your code examples.

Right now, the class Connexion is not declared 'static; why ?

If it's not declared static, show the constructor (if any), and show the code in your Form that creates a new instance of Connexion.

Don't store a SqlConnection in a static field. That will leads to many hard-to-diagnose problems with your code.

Instead, you should create the connection when it's required, and wrap it in a using block to ensure that it's always disposed of properly.
C#
static class Connexion
{
    public static SqlConnection CreateConnection(string databaseName)
    {
        return new SqlConnection($"Data Source=***\\DB; Initial Catalog={databaseName}; Persist Security Info=False; User ID=***; Password=***");
    }
}
C#
using (SqlConnection connection = Connexion.CreateConnection(theDatabaseNameToUse))
using (SqlCommand command = new SqlCommand("...", connection))
{
    ...
}
 
Share this answer
 
You shouldn't hardcode any p[art of a connection string, it can lead to problems in production - and you definitely shouldn't be using the same server for production and test as a trivial error on your part can completely ruin your whole day ...

So instead of doing that, have a look at this: Instance Storage - A Simple Way to Share Configuration Data among Applications[^] - it's a little overkill for what I see of your app, but it's how I manage the problem and avoid hardcoding at the same time.
 
Share this answer
 
You need to have a constructor for your class that takes a parameter. See the documentation Using Constructors - C# Programming Guide | Microsoft Docs[^] and scroll down or search for
C#
public Employee(int annualSalary)
 
Share this answer
 
Comments
BillWoodruff 28-Oct-21 8:10am    
how is this relevant to the question asked ?
CHill60 28-Oct-21 8:26am    
They stated "i want to pass the information to this class"

Edit: If it's the "public Employee..." that concerns you that was an indication of where to go in the article to find how to create a class constructor that takes parameters
BillWoodruff 28-Oct-21 8:57am    
Voted #1: this is a vague response that ignores the equally vague question, that invents a hypothetical "Employee" class the OP never mentions, and ignores the possible implications of the fact the one method the OP shows us is declared 'static.

imho, my esteemed colleague CHill is way too smart to have posted this.
CHill60 28-Oct-21 9:05am    
You clearly did not read my response to your comment. There is nothing "vague" about the response "You need to have a constructor for your class that takes a parameter". You will note that Richard Deeming has provided a solution that has a constructor that takes parameters.
The link is to a Microsoft docs article, to an article that explains how to add constructors that take parameters to classes. By searching for (hint: "search for" is an instruction to search for something) "public Employee(int annualSalary)" the OP would find the exact spot in the article that answers their needs.
Your reaction is a perfect example of responding to a post without actually bothering to read it in full
BillWoodruff 28-Oct-21 9:12am    
I read your post in full, and your comment in full. And, i never down-vote a poster with your track record on CP without very careful consideration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900