Home > Net >  With c # assignment variables effect
With c # assignment variables effect

Time:11-17

With c # assignment variables effect problem

//create a class in c #
The class Conn
{
Private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
Public static SqlConnection rs=new SqlConnection (cn);
Public static string PSWD;
}


//create a form
Private void Form1_Load (object sender, EventArgs e)
{
Conn. PSWD="abc123".

String SQL="Select * From tTable Where tID='1'";
SqlCommand CMD=new SqlCommand (SQL, Conn. Rs);
Conn. Rs. The Open ();
SqlDataReader Dr=CMD. ExecuteReader ();
Dr. Read ();
. This Text=Dr [r]. "LoginTitle" ToString ();
Dr. Close ();
CMD. The Clone ();
Conn. Rs. The Close ();
}


Consult everybody, why don't the password assigned to take effect,
Assignment in the form Form1_Load Conn. PSWD="abc123" password is still not effective, how to take effect,
I want to be a global variable Conn. PSWD, let all form Form1, Form2, Form3.. Also keep this password,
Password is abc123 was taken out of other database,
Consult everybody, help to look at, thank you!

CodePudding user response:

Object rs definition has been initialized when members are static (rs), use the password is not the PSWD assignment; Then you give PSWD assignment, but it won't update the rs connection string, you need to generate a new connection string, the simple way is to set members PSWD to attribute rather than a simple variable, and then modify the attributes using code to regenerate the connection string,

CodePudding user response:

Ask how to modify the above code, only can be solved?

CodePudding user response:

This all have no, you must use the

 
The class Conn
{
Public static SqlConnection rs;
Public static void SetPwd (string PWD)
{
Rs=new SqlConnection (" Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PWD);
}
}

CodePudding user response:

In a static variable Conn. PSWD assignment,
(string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ Conn. PSWD) This code should be written in the current window Form1_Load,
If you want to use global Conn. PSWD, and need a separate assignment in different forms, so the connection string cn can't written in the Conn class static, you need to regenerate the connection strings in the current window cn,
If, all written in the Conn class static, then it must be ascribed the value in the Conn class,

CodePudding user response:

I ask 2 floor, my private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
This is a private

CodePudding user response:

reference 5 floor xiehuangda reply:
consult 2 floor, my private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
This is a private

Why do you try this and have no effect on must be private and not used in other places

CodePudding user response:

3 floor should be a positive solution

CodePudding user response:

Private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
This must be used as a private good,
Ask what method, and to influence more than other things?
Such as public static string Uk {get; The set; }

CodePudding user response:

refer to the eighth floor xiehuangda response:
private static string cn="Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
This must be used as a private good,
Ask what method, and to influence more than other things?
Such as public static string Uk {get; The set; }

3/f method is not quite good, why insist? You are not to write garbage code? Give you a tall and straight poplar trees do not, you will be crooked neck tree, don't take that attitude to do project, that is to dig a pit for others,

CodePudding user response:

Public static void SetPwd (string PWD)
{
Rs=new SqlConnection (" Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PWD);
}

I ask 2 floor, this how to set up into more private project, not suitable for the public

CodePudding user response:

Don't teach bad new, keep any unnecessary in the class is open to the outside world access method and the process is private is a good habit, let alone a such thing as a database connection string,

C # the visitor is very nice, do you want to consider also can use static, the get accessor ensures that every time want to get the value of the cn will take a look at your connection password,

The code is as follows:

 class Conn 
{
Private static string cn
{
The get
{
Return "Data Source=192.168.1.8, 1433; Initial Catalog=Data2020; User ID=sa; The PWD="+ PSWD;
}
}
Public static SqlConnection rs=new SqlConnection (cn);
Public static string PSWD;
}

CodePudding user response:

Thanks for the support of mixing the raw 11 floor, private really specification,
After testing your code, found the password after the assignment or not to take effect,
Assignment in the form Form1_Load Conn. PSWD="abc123" password or not to take effect, how the code change to solve it,
I want to do is just in the form Form1 assignment after password, other forms can be keep using this password,
Because, Form1 was taken out of other database password, and then assign values to the PSWD, Form2, Form3.. All forms can be public,
Consult everybody how to solve, thank you!

CodePudding user response:

refer to 12 floor xiehuangda reply:
thanks for the support of mixing the raw 11 floor, private really specification,
After testing your code, found the password after the assignment or not to take effect,
Assignment in the form Form1_Load Conn. PSWD="abc123" password or not to take effect, how the code change to solve it,
I want to do is just in the form Form1 assignment after password, other forms can be keep using this password,
Because, Form1 was taken out of other database password, and then assign values to the PSWD, Form2, Form3.. All forms can be public,
Consult everybody how to solve, thank you!


Do you want to understand
 public static SqlConnection rs=new SqlConnection (cn); 

You a piece of code, because it is a static object, so before you form1 Load method to execute, he finished, the object is instantiated so when instantiated PSWD is empty, the subsequent even if you go to modify the PSWD content, your rs object is also won't have any change,
It's like you go to a bank to withdraw money, you say to the ATM "took the card all the money in my wallet!" Due to your card have no money, and then a dime in your wallet at all,
Then you online banking transfer $100 in the bank card, your bank card at this time there are 100 pieces, but your wallet is empty, still here your rs is your money in the wallet,
PSWD is your bank card,
So to achieve your needs, your logic should be saving money in the bank card first, and then take money, or do you want for PSWD assignment first, and then instantiate the rs objectnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related