//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