Can';我不知道如何使用SQL数据源ASP.Net将输出的参数存储到变量中
我正在开发一个应用程序,它要求我查找登录用户的用户名,查询用户名=活动用户的employee表,然后返回EmployeeID、TeamID和PositionID以设置会话状态变量,从而根据每个用户创建不同的视图 我创建了这个存储过程来输出所需的参数Can';我不知道如何使用SQL数据源ASP.Net将输出的参数存储到变量中,asp.net,sql-server,session-state,Asp.net,Sql Server,Session State,我正在开发一个应用程序,它要求我查找登录用户的用户名,查询用户名=活动用户的employee表,然后返回EmployeeID、TeamID和PositionID以设置会话状态变量,从而根据每个用户创建不同的视图 我创建了这个存储过程来输出所需的参数 CREATE PROCEDURE ASP_UserSession @Username nvarchar(256), @UserID int OUTPUT, @TeamID int OUTPUT, @PositionID int OUTPUT AS
CREATE PROCEDURE ASP_UserSession @Username nvarchar(256), @UserID int OUTPUT, @TeamID int OUTPUT, @PositionID int OUTPUT
AS
SELECT @UserID = ID, @TeamID = TeamID, @PositionID = PositionID
FROM Employee
WHERE UserName = @Username
现在的问题是将输出的参数存储到代码隐藏文件中,以便将参数设置为会话状态
我准备把我所有的头发都拔出来了!请帮忙 DbParameter类有一个
Direction
属性,您可以设置ParameterDirection.Output的值
执行存储过程后,从命令对象的parameters集合中获取parameter对象并获取其值。我能够用以下代码解决我的问题:
MembershipUser user = Membership.GetUser();
string activeuser = user.UserName;
using (System.Data.SqlClient.SqlConnection sc1 =
new System.Data.SqlClient.SqlConnection(@"Data Source=DRIFT\GANDALF;Initial Catalog=Wizard_Swears;" +
"Integrated Security=True"))
{
sc1.Open();
using (System.Data.SqlClient.SqlCommand command1 = new System.Data.SqlClient.SqlCommand())
{
command1.CommandType = CommandType.Text;
command1.Connection = sc1;
// DIRECTION :: Input
command1.CommandText = "select @UserID = [ID], @TeamID = [TeamID], @PositionID = [PositionID] FROM [Employee] WHERE ([UserName] = @UserName)";
System.Data.SqlClient.SqlParameter paramter1 = command1.Parameters.Add("@UserName", activeuser);
System.Data.SqlClient.SqlParameter paramter2 = command1.Parameters.Add("@UserID", SqlDbType.SmallInt);
System.Data.SqlClient.SqlParameter paramter3 = command1.Parameters.Add("@TeamID", SqlDbType.SmallInt);
System.Data.SqlClient.SqlParameter paramter4 = command1.Parameters.Add("@PositionID", SqlDbType.SmallInt);
paramter1.Direction = ParameterDirection.Input;
paramter2.Direction = ParameterDirection.Output;
paramter3.Direction = ParameterDirection.Output;
paramter4.Direction = ParameterDirection.Output;
command1.ExecuteNonQuery();
//Store values to variables to be set to session
string userID = paramter2.Value.ToString();
string teamID = paramter3.Value.ToString();
string positionID = paramter4.Value.ToString();
UserIDLBL.Text = userID;
TeamIDLBL.Text = teamID;
PositionIDLBL.Text = positionID;
Session["Username"] = activeuser;
Session["UserID"] = UserIDLBL.Text;
Session["AMID"] = TeamIDLBL.Text;
Session["PositionID"] = PositionIDLBL.Text;
我已经试过了,但由于某些原因,我无法让这些输出参数工作。有什么问题吗?请尝试设置ParameterDirection.Output。我曾经遇到过一些inputoutput对我不起作用的例子,但我可能又犯了一些错误。哦,是的,因为参数不用于输入,所以设置为output您有关于如何将输出存储到代码中的变量的示例代码吗?