Home > Back-end >  C # to write the login interface, with the data in the database, input is correct, but always prompt
C # to write the login interface, with the data in the database, input is correct, but always prompt

Time:09-25

 private void btnLogin_Click (object sender, EventArgs e) 
{

If (this. TxtUserName. Text. The Trim ()=="")
{
MessageBox. Show (" user name cannot be empty ", "prompt");
return;
}

//the connection string
String STR="server=localhost; The database=addressBook; Uid=sa; The PWD=123456 ";

//connection object
SqlConnection con=new SqlConnection ();
Con. The ConnectionString=STR;

//open the database
Con. The Open ();

//execute the command

//CMD. Connection=con;

The DataSet ds=new DataSet ();
String st="select the User, the Password from the users where the User='" + txtUserName. Text. The ToString (). The Trim () +"' and Password='" + txtPassword. Text. The ToString (). The Trim () + "' ";
SqlCommand CMD=new SqlCommand (st, con);
SqlDataReader Dr=CMD. ExecuteReader ();
Try
{

If (Dr. Read ())
{
This. Hide ();
MainForm FRM=new MainForm ();
FRM. The Show ();
}


The else
{
MessageBox. Show (" account and password error, please input again!" , "tip");
TxtPassword. Text="";
Enclosing the Focus ();
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
The finally
{

CMD. The Dispose ();
Con. The Dispose ();
}
}

CodePudding user response:

Can set static connection test successful inside attribute? Don't write code to connect,

CodePudding user response:

Tried, or that problem, database connection: no problem

CodePudding user response:

Estimation is the query string concatenation appear a mistake, do not recommend this recklessly, suggested parameter binding way,
You can put your query string output look,
  • Related