链接到SQL Server Express的C#输出错误
下面是我按下按钮后得到的输出,尽管我的SQL Server数据库中确实存在链接到SQL Server Express的C#输出错误,c#,sql-server,sql-server-express,C#,Sql Server,Sql Server Express,下面是我按下按钮后得到的输出,尽管我的SQL Server数据库中确实存在EmployeeNumber和DOB 我尝试了很多次,但都无法找出导致输出错误的问题 谢谢你的帮助 C#代码: SQL Server存储过程代码: CREATE PROCEDURE spEmployeeC @Employee INT, @DOB DATE AS BEGIN SET NOCOUNT ON; DECLARE @Employee_check INT; DECLARE
EmployeeNumber
和DOB
我尝试了很多次,但都无法找出导致输出错误的问题
谢谢你的帮助
C#代码:
SQL Server存储过程代码:
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);
}
}
还可以更改存储过程以返回记录计数:
CREATE PROCEDURE [dbo].[spValidateCredentials]
@Employee INT,
@DOB DATE
AS
BEGIN
SELECT COUNT(*)
FROM Employee_Table
WHERE EMPLOYEE_NUM=@Employee
AND DOB=@DOB
END
第一:第二:您不会对存储过程执行的结果做任何事情。您只需检查用户名文本框是否包含文本“@Employee”@DavidG Hi David!感谢您的分享,有没有更好的方法来改进我的代码?:)我不想听起来很难听,但这是非常基本的C。我建议您在这里提出更多问题之前先浏览一些教程。@DavidG我明白了,很抱歉打扰了tho。我今天刚开始写我的C代码。好的,谢谢,顺便说一句:)
CREATE PROCEDURE [dbo].[spValidateCredentials]
@Employee INT,
@DOB DATE
AS
BEGIN
SELECT COUNT(*)
FROM Employee_Table
WHERE EMPLOYEE_NUM=@Employee
AND DOB=@DOB
END