Retrieving a record from a stored procedure in asp.net

I am very new to ASP.NET, so I apologize for the naive question, but I'm just wondering how I can get the data from the stored procedure that I'm calling from ASP.NET. The stored procedure should return a single row, and I want to get the returned record fields.

So this is what I still came up with

Stored procedure

ALTER PROCEDURE dbo.StoredProcedure6

@LoginName varchar(50)

AS
 SELECT username ,Password  FROM dbo.Users
 Where username = @LoginName
RETURN

Code for accessing a specific entry in the asp.net.cs file

var user = dbcontext.returnuserdetails(txtEmailAddress.Text); 

where returnuserdetails is the function that I added through the model browser in Visual Studio 2010

Now the question is, how to get and save the values ​​of the returned username and password?

I work in ASP.NET 4.0 if that helps.

thank

+3
source share
2 answers

4.0, LINQ to SQL, .

private void GetUser(string emailAddress){

  using(DataContext dbcontext = new DataContext()){
    var AppData.user = dbcontext.users
             .Select(u => u.email_address == emailAddress).SingleOrDefault();

    // access entity properties as needed
    // user.email_address, user.first_name, etc..

  }    

}

, , , .

, LINQ-to-SQL .

ALTER PROCEDURE dbo.ReturnUserDetails

@LoginName varchar(50)

AS
 SELECT * -- Get whole row so you have all info that is possibly needed  
 FROM dbo.Users
 Where username = @LoginName
RETURN

#

private void GetUser(string userName){

      using(DataContext dbcontext = new DataContext()){
        var user = dbcontext.ReturnUserDetails(userName).SingleOrDefault();

        // access entity properties as needed
        string userName =  user.username;
        var password = user.Password;

      }    
}
+2

SqlDataReader

: , , StoredProcedure.
, .

private static void ReadOrderData(string connectionString)
{
    string queryString =
        "SELECT OrderID, CustomerID FROM dbo.Orders;";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }

        // Call Close when done reading.
        reader.Close();
    }
}
+1

All Articles