简体   繁体   中英

Get Multiple Values From Database ASP.NET/C#

I am trying to get/return multiple values from an SQL-Server database using and display them on an ASP.NET page.

I am using a stored procedure to perform the SELECT command on the Database side.

I am able to return the first value that matches the variable @PERSON but only one row is returned each time.

Any help would be much appreciated.

Database handler class

public MainSQL()
{
       _productConn = new SqlConnection();
       _productConnectionString += "data source=mssql.database.co.uk;InitialCatalog=test_data;User ID=username;Password=password";
       _productConn.ConnectionString = _productConnectionString;
}
public string GetItemName(int PersonID)
{
     string returnvalue = string.Empty;
     SqlCommand myCommand = new SqlCommand("GetItem", _productConn);
     myCommand.CommandType = CommandType.StoredProcedure;
     myCommand.Parameters.Add(new SqlParameter("@PERSON", SqlDbType.Int));
     myCommand.Parameters[0].Value = PersonID;
     _productConn.Open();
     returnvalue = (string)myCommand.ExecuteScalar();             
     _productConn.Close();
     return (string)returnvalue;
 }

Stored Procedure

USE [test_data]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [ppir].[GetItem]    
    (
    @PERSON int
    )   
AS
    /*SET NOCOUNT ON;*/
    SELECT Description FROM [Items] WHERE PersonID = @PERSON
    RETURN

return.aspx

namespace test
{
    public partial class Final_Page : System.Web.UI.Page
    {
        MainSQL GetInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            int PersonId = (int)Session["PersonID"];
            GetInfo = new MainSQL();
            string itemname = GetInfo.GetItemName(PersonId);
            ReturnItemName.Text = itemname;

        } // End Page_Load

    } // End Class 

} // End Namespace

you should use sql datareader instead.:

ExecuteScalar returns you only the first result while reader returns you each result by loop until reader.Read()==false.

eg :

 DataReader data_reader= MySqlCommand.ExecuteReader( ); 
   while(data_reader.Read())  
  {
       ...
  }         

I Change your GetItem method like this :

public List<string> GetItemName(int PersonID)
{
   List<string> returnvalues = new List<string>();
   SqlCommand myCommand = new SqlCommand("GetItem", _productConn);
   myCommand.CommandType = CommandType.StoredProcedure;
   myCommand.Parameters.Add(new SqlParameter("@PERSON", SqlDbType.Int));
   myCommand.Parameters[0].Value = PersonID;

   _productConn.Open();
   DataReader dr = myCommand.ExecuteReader(); 
   While(dr.Read() )  
   {
       returnvalues.Add(dr[0].ToString()); 
   }         

   _productConn.Close();
   return returnvalues;
 }

Does your stored procedure return one row for the id or does it return multiple rows? Ultimately you will need to loop over your results. If the stored procedure returns one record per call, then you need to loop over the ids in the aspx page. If the stored procedure returns multiple rows, then you can use and sqlDataReader instead of the ExecuteScalar call. Loop over the rows that were return and add them to some sort of a collection or list. Then return it to your page. You will still have to modify your aspx page to handle the collection, however.

你可以使用一个SqlDataReader的 ,或SqlDataAdapter的填写一个DataSet ,虽然对于web表单,你可能会更好地通过完全由使用的方式分离您的网页数据访问服务的ObjectDataSource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM