私は電子メールとパスワードを一致させるために3つのテーブルの行をカウントするストアドプロシージャを作成しました。ストアドプロシージャ:私のストアドプロシージャは、コードの背後で実行されたとき常にゼロを返します
ALTER PROCEDURE dbo.StoredProcedure1
(
@Email nvarchar(50),
@pass nvarchar(50),
@outres int = null OUTPUT,
@outres1 int = null OUTPUT,
@outres2 int = null OUTPUT,
@outres3 int = null OUTPUT
)
AS
DECLARE @UserId INT
set @outres1 = (SELECT count(*) FROM [dbo].Admins
WHERE [email protected] AND [email protected])
if (@outres1=1)
begin
set @[email protected]
SELECT @UserId = adminID
FROM [dbo].Admins WHERE [email protected] AND [email protected]
set @outres1= @UserId
RETURN @outres
end
set @outres2= (SELECT count(*) FROM [dbo].Instructors
WHERE [email protected] AND [email protected])
if (@outres2=1)
begin
set @[email protected]
SELECT @UserId = instructorID
FROM [dbo].Instructors
WHERE [email protected] AND [email protected]
set @outres2= @UserId
RETURN @outres
end
set @outres3= (SELECT count(*) FROM [dbo].Students
WHERE [email protected] AND [email protected])
if (@outres3=1)
begin
set @outres= @outres3
SELECT @UserId = studentID
FROM [dbo].Students
WHERE [email protected] AND [email protected]
set @outres3= @UserId
RETURN @outres
end
else
begin
set @[email protected]
set @outres1=0
set @outres2=0
set @outres3=0
RETURN @outres
end
私は背後にあるコード..hereだことはできませんブラウザでログインしようとすると、..and見つかった場合、それはまた、ユーザーのユーザーIDを保存します。
public int Validate_Login(String _Email, String _Password)
{
string lbl;
string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connstr);
SqlCommand cmdselect = new SqlCommand();
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = "[dbo].[StoredProcedure1]";
cmdselect.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = _Email;
cmdselect.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = _Password;
cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
cmdselect.Parameters.Add("@OutRes1", SqlDbType.Int, 4);
cmdselect.Parameters.Add("@OutRes2", SqlDbType.Int, 4);
cmdselect.Parameters.Add("@OutRes3", SqlDbType.Int, 4);
cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
cmdselect.Parameters["@OutRes1"].Direction = ParameterDirection.Output;
cmdselect.Parameters["@OutRes2"].Direction = ParameterDirection.Output;
cmdselect.Parameters["@OutRes3"].Direction = ParameterDirection.Output;
cmdselect.Connection = con;
int Results = 0;
try
{
con.Open();
// SqlDataReader rd = cmdselect.ExecuteReader();
cmdselect.UpdatedRowSource = UpdateRowSource.OutputParameters;
cmdselect.ExecuteNonQuery();
//cmdselect.ExecuteScalar();
/* if (rd.HasRows)
{
rd.Read();
FormsAuthentication.RedirectFromLoginPage(ETB.Text, true);
Response.Redirect("Welcome.aspx");
}
else
{
lblmsg.Text = "Invalid username or password.";
}*/
Results = (int)cmdselect.Parameters["@OutRes"].Value;
int res1 = (int)cmdselect.Parameters["@OutRes1"].Value;
int res2 = (int)cmdselect.Parameters["@OutRes2"].Value;
int res3 = (int)cmdselect.Parameters["@OutRes3"].Value;
if (Results == 0)
{
Session["userID"] = null;
}
else
{
if (res1 != 0)
{
Session["userID"] = res1.ToString();
}
else if (res2 != 0)
{
Session["userID"] = res2.ToString();
}
else
{
Session["userID"] = res3.ToString();
}
}
}
catch (SqlException ex)
{
lbl = ex.Message;
}
finally
{
cmdselect.Dispose();
if (con != null)
{
con.Close();
}
}
return Results;
}
は、コード内のすべてのコメントを試したが、この関数は常にゼロを返します。 .please help ...
私はいつもExecuteReaderを使用して、出力パラメータを読み取る前にReaderを閉じました。最後に、spでSELECTを使用して出力パラメータを保存します。あなたの条件はテストするのが難しいので、私はこれを提案として残し、何が起こるか見てみましょう。ところで、RETURN @outresは機能しません。パラメータの方向はReturnValueでなければなりません – Steve