Home > Net >  Novice consult a SqlParameter problem
Novice consult a SqlParameter problem

Time:10-19

The users table:
The CREATE TABLE [dbo] [sys_user] (
[ID]/int IDENTITY (1, 1) NOT NULL,
[userName] [nvarchar] (50) NULL,
[userPass] [nvarchar] (50) NULL,
PRIMARY KEY CONSTRAINT [PK_sys_user] CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX=OFF, STATISTICS_NORECOMPUTE=OFF, IGNORE_DUP_KEY=OFF, ALLOW_ROW_LOCKS=ON, ALLOW_PAGE_LOCKS=ON) ON (PRIMARY)
) ON the (PRIMARY)

GO

Cs file:
Private void btnLogin_Click (object sender, EventArgs e)
{
Var userName=txtuser. Text. The Trim ();
Var userPass=txtPass. Text. The Trim ();
String SQL="SELECT count (*) FROM sys_user WHERE username=@ username AND userpass=@ userpass";

SqlParameter PMT=new SqlParameter [] [] {
New SqlParameter (" @ the userName ", SqlDbType NVarChar, 50) {Value=https://bbs.csdn.net/topics/userName},
New SqlParameter (" @ userPass ", SqlDbType NVarChar, 50) {Value=https://bbs.csdn.net/topics/userPass}
};

Int r=the Convert. ToInt32 (SqlHelper ExecuteNonQuery (SQL, PMT));
If (r & gt; 0)
{
MessageBox. Show (" login success ");
}
The else
{
MessageBox. Show (" login failed ");
}

}

SqlHelper file:
Public static int ExecuteNonQuery (string SQL, params SqlParameter [] PMS)
{
Using (SqlConnection conn=new SqlConnection (conStr))
{
Using (SqlCommand CMD=new SqlCommand (SQL, conn))
{
If (PMS!=null)
{
CMD. The Parameters. AddRange (PMS);
}
Conn. The Open ();
Return CMD. ExecuteNonQuery ();
}
}
}

Question:
No matter what kind of a user name, password input, always pop-up login failed,
Quick depressed,
Ask ace to give directions where is wrong?

CodePudding user response:

ExecuteNonQuery returns the number of rows affected if the execution of the select statement returns always - 1

CodePudding user response:

 
Using (SqlDataReader reader=CMD. ExecuteReader ())
If (reader. The Read ())
Return reader. GetInt32 (0);

CodePudding user response:

ExecuteNonQuery () returns affect the number of records for deletion, query, generally with SqlDataAdapter, check on the net, a lot of examples

CodePudding user response:

reference 1st floor jinkuang45 response:
ExecuteNonQuery returns the number of rows affected, if you execute the select statement returns always - 1


Thank you for the great god, I know why
  •  Tags:  
  • C#
  • Related