C# 链接到 SQL Server Express 的错误输出
Wrong output for C# linking to SQL Server Express
下面是我按下按钮后得到的输出,尽管 EmployeeNumber
和 DOB
确实存在于我的 SQL 服务器数据库中。
我已经试了很多次了,没能找出导致错误输出的问题。
感谢您的帮助!
C#代码:
private void btn_Click(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["FirstConnection"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("spEmployeeC", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Employee", txtEmployee.Text);
cmd.Parameters.AddWithValue("@DOB", txtPassword.Text);
//cmd.IsValid
con.Open();
cmd.ExecuteNonQuery();
if (txtEmployee.Text == "@Employee" && txtPassword.Text == "@DOB")
{
MessageBox.Show("Welcome " + txtEmployee.Text);
}
else
{
MessageBox.Show("The Username or Password you entered is incorrect. Please try again");
}
}
SQL 服务器存储过程代码:
CREATE PROCEDURE spEmployeeC
@Employee INT,
@DOB DATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Employee_check INT;
DECLARE @Password_check DATE;
SELECT *
FROM Employee_Table
WHERE [EMPLOYEE_NUM] = @Employee AND [DOB] = @DOB
END
像这样会更好:
protected void btn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtEmployee.Text))
{
MessageBox.Show("You must supply an Employee Number");
return;
}
if (string.IsNullOrEmpty(txtPassword.Text))
{
MessageBox.Show("You must supply a Password");
return;
}
if (IsAuthenticated())
{
MessageBox.Show("Welcome " + txtEmployee.Text);
}
else
{
MessageBox.Show("The Username or Password you entered is incorrect. Please try again");
};
}
private bool IsAuthenticated()
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("spValidateCredentials", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Employee", SqlDbType.Int).Value = txtEmployee.Text;
cmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DateTime.Parse(txtPassword.Text);
conn.Open();
return ((int)cmd.ExecuteScalar() > 0);
}
}
也可以将存储过程更改为 return 记录数:
CREATE PROCEDURE [dbo].[spValidateCredentials]
@Employee INT,
@DOB DATE
AS
BEGIN
SELECT COUNT(*)
FROM Employee_Table
WHERE EMPLOYEE_NUM=@Employee
AND DOB=@DOB
END
下面是我按下按钮后得到的输出,尽管 EmployeeNumber
和 DOB
确实存在于我的 SQL 服务器数据库中。
我已经试了很多次了,没能找出导致错误输出的问题。
感谢您的帮助!
C#代码:
private void btn_Click(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["FirstConnection"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("spEmployeeC", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Employee", txtEmployee.Text);
cmd.Parameters.AddWithValue("@DOB", txtPassword.Text);
//cmd.IsValid
con.Open();
cmd.ExecuteNonQuery();
if (txtEmployee.Text == "@Employee" && txtPassword.Text == "@DOB")
{
MessageBox.Show("Welcome " + txtEmployee.Text);
}
else
{
MessageBox.Show("The Username or Password you entered is incorrect. Please try again");
}
}
SQL 服务器存储过程代码:
CREATE PROCEDURE spEmployeeC
@Employee INT,
@DOB DATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Employee_check INT;
DECLARE @Password_check DATE;
SELECT *
FROM Employee_Table
WHERE [EMPLOYEE_NUM] = @Employee AND [DOB] = @DOB
END
像这样会更好:
protected void btn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtEmployee.Text))
{
MessageBox.Show("You must supply an Employee Number");
return;
}
if (string.IsNullOrEmpty(txtPassword.Text))
{
MessageBox.Show("You must supply a Password");
return;
}
if (IsAuthenticated())
{
MessageBox.Show("Welcome " + txtEmployee.Text);
}
else
{
MessageBox.Show("The Username or Password you entered is incorrect. Please try again");
};
}
private bool IsAuthenticated()
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("spValidateCredentials", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Employee", SqlDbType.Int).Value = txtEmployee.Text;
cmd.Parameters.Add("@DOB", SqlDbType.Date).Value = DateTime.Parse(txtPassword.Text);
conn.Open();
return ((int)cmd.ExecuteScalar() > 0);
}
}
也可以将存储过程更改为 return 记录数:
CREATE PROCEDURE [dbo].[spValidateCredentials]
@Employee INT,
@DOB DATE
AS
BEGIN
SELECT COUNT(*)
FROM Employee_Table
WHERE EMPLOYEE_NUM=@Employee
AND DOB=@DOB
END