Home > Net >  Vs login screen enter your user name password is correct, still login fails
Vs login screen enter your user name password is correct, still login fails

Time:09-22

The data layer code
The namespace YNCCommonSystem. DAL
{
Public class BaseUserService
{
//information query users based on user name
Public static DataTable GetUserInfoByUserName (string userName)
{
//to establish links with the database
//1. The connection string
String connString=@ "Data Source=WIN - 4 jhp4b1tfu4; Initial Catalog=YNCSystem; User ID=sa; Password=* * * * * * * * * * ";
/password has been changed here
//2. Create links by link string objects
SqlConnection conn=new SqlConnection (connString);
Try
{
//to execute SQL statements
String SQL=the string. Format (@ "select * from [YNCSystem] [dbo] [User] where the userName='{0}'", userName);
//3. Open the link
conn.Open();
//executing SQL statements for data
//execute SQL command
SqlCommand CMD=new SqlCommand (SQL, conn);
//get data into memory warehouse
SqlDataAdapter dataadapters=new SqlDataAdapter (CMD);
//create a temporary warehouse
The DataSet ds=new DataSet ();
Dataadapters. The Fill (ds);
//return to data
Return the ds. Tables [0];
}
The catch (Exception ex)
{
Throw the ex.
}
The finally
{
//close link
conn.Close();
}
}
}
}
The business logic layer code:
The namespace YNCCommonSystem. BLL
{
//the user table of business logic layer
Public class BaseUserManager
{
//1. Implementation system login
Public static bool Login (string strLoginName, string strLoginPwd)
{
Try
{
//1. Call the data access layer, according to the user name to get user information
The DataTable dtUser=BaseUserService. GetUserInfoByUserName (strLoginName);
If (dtUser. Rows. Count> 0)
{
DataRow drUser=dtUser. Rows [0];
//2. The password of user information compared with the presentation layer password
If (drUser [r]. "Code" Equals (strLoginPwd))
{
return true;
}
The else
{
return false;
}
}
The else
{
return false;

}
}
The catch (Exception ex)
{
Throw the ex.
}

}

}
}
The presentation layer code
The namespace WinYNC
{
Public partial class Formlogin: Form
{
Public Formlogin ()
{
InitializeComponent();
}

Private void label2_Click (object sender, EventArgs e)
{

}

Private void btnCancel_Click (object sender, EventArgs e)
{
//close the form
//this. Close;
//exit the application
DialogResult Dr=MessageBox. Show (" exit the prompt ", "are you sure you want to exit the application?" , MessageBoxButtons. YesNo, MessageBoxIcon. Question);
If (Dr==System. Windows. Forms. DialogResult. Yes)
{
Application. The Exit ();
}


}

Private void btnLogin_Click (object sender, EventArgs e)
{
//call the login method of business logic layer
Bool ret=BaseUserManager. Login (enclosing tbUserName. Text, enclosing tbPwd. The Text).
If (ret==true)
{
MessageBox. Show (" login successful!" );
}
The else
{
MessageBox. Show (" login failed!" );
}
}
}
}
Database:

Run according to logon failure after enter the user name password, set breakpoints when the string connString=@ "Data Source=WIN - 4 jhp4b1tfu4; Initial Catalog=YNCSystem; User ID=sa; Password=* * * * * * * * * * "; There is an error, but I don't know how to change?

CodePudding user response:

You'd put the content of the error come out ah, you do not try the computer name by IP address

CodePudding user response:

Look at your table at the back of the data selected is Spaces, nchar type are you? You directly in the SQL Server Management Studio to try check in

CodePudding user response:

Database field type change, varChar field definition to instead, or written to the database string, the string on the Trim (), such as aa. The Trim ().

CodePudding user response:

A habit, read write string, the back to add the Trim ()

CodePudding user response:

There are too many questions about the database, often error information is too far away from the problem, it is a waste of time, I get method is to generate database, problems to be resolved immediately, and then regular database backup, restore database problems, if recovery is not successful to regenerate the database,
  •  Tags:  
  • C#
  • Related