EmployeeTrackDatacontextDataContext db = new EmployeeTrackDatacontextDataContext();
DataSet logStatusDs = new DataSet();
我有一个数据集:
public DataSet Sequence(Int32 userId)
{
//load the List one time to be used thru out the intire application
var ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["EMSJConnectionString"].ConnectionString;
using (SqlConnection connStr = new SqlConnection(ConnString))
{
using (SqlCommand cmd = new SqlCommand("get_LoginStatusQ", connStr))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Connection.Open();
new SqlDataAdapter(cmd).Fill(logStatusDs);
//DataTable dt = logStatusDs.Tables[0].Rows[0];
}
}
return logStatusDs;
}
我使用的存储过程是:
ALTER PROCEDURE [dbo].[get_LoginStatusQ]
@UserId int
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 1 LoginStatus
FROM UserTime
WHERE UserId = 2
ORDER BY UserTimeId DESC
END
返回此数据:
LoginStatus
________________
1 | In |
方法
Sequence(Convert.ToInt32(Session["userid"]))
我在PageLoad
上运行它在这个方法之后,我想从存储过程中获取返回值,即" In"并在IF语句中进行比较。我怎么能这样做?
前:
If(ReturnValueOfStoredProcedure != "In")
{ do stuf...}
else
{ do stuff... }
示例将不胜感激。
答案 0 :(得分:0)
您只是从查询中返回单个值。这可以使用ExecuteScalar而不是构建和填充DataSet及其表
来完成public string Sequence(Int32 userId)
{
//load the List one time to be used thru out the intire application
var ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["EMSJConnectionString"].ConnectionString;
using (SqlConnection connStr = new SqlConnection(ConnString))
{
using (SqlCommand cmd = new SqlCommand("get_LoginStatusQ", connStr))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Connection.Open();
object result = cmd.ExecuteScalar();
return result == null ? string.Empty : result.ToString()
}
}
}
现在电话只是
string status = Sequence(Convert.ToInt32(Session["userid"]))
if(status == "In")
{
}
else
{
}
唯一要查找的是ExecuteScalar的返回值,如果查询找不到具有给定ID的用户,则该值可能为null