Вопрос

I have a stored procedure for getting last row value of a UserId:

ALTER PROCEDURE [dbo].[get_LoginStatusQ]  
    @UserId int
AS   
BEGIN  
   SELECT MAX(UserTimeId), LoginStatus 
   FROM UserTime 
   WHERE UserId = @UserId 
   GROUP BY UserId, LoginStatus, Day, HoursWorked, Date, CheckIn,UserTimeId 
END

In C#, I want to get the value of LoginStatus. How to do I do this?

I tried:

public void GetLogin(object sender, EventArgs e)
{ 
   db.get_LoginStatusQ(Convert.ToInt32(Session["userid"])));

   if( LoginStatus ='In') // How do I get the field LoginStatus from the database?
   {
   }
}
Это было полезно?

Решение

for your C# code try something like this make sure that in your using you have the following you can change the declaration of the Method signature to Private if you do not have a DAL Class set up of your own.

using System.Data;
using System.Data.SqlClient;

    public static DataSet GetLogin(Int32 userId)
    {
       DataSet logStatusDs = new DataSet();
        //load the List one time to be used thru out the intire application
        var ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["CMSConnectionString"].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);
            }
        }
        return logStatusDs;
    }   

Другие советы

Your stored procedure needs some fixing see below:

ALTER PROCEDURE [dbo].[get_LoginStatusQ]  
@UserId int
AS   
BEGIN  
 SET NOCOUNT ON;

    SELECT TOP 1 UserTimeId, LoginStatus 
    FROM UserTime 
    WHERE UserId = @UserId 
    ORDER BY UserTimeId DESC
END
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top