[英]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. 我正在尝试使用SQL Server数据库获取/返回多个值,并将其显示在ASP.NET页上。
I am using a stored procedure to perform the SELECT command on the Database side. 我正在使用存储过程在数据库端执行SELECT命令。
I am able to return the first value that matches the variable @PERSON but only one row is returned each time. 我能够返回与变量@PERSON匹配的第一个值,但每次仅返回一行。
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 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.: 您应该改用sql datareader。
ExecuteScalar returns you only the first result while reader returns you each result by loop until reader.Read()==false. ExecuteScalar仅返回第一个结果,而Reader则循环循环返回每个结果,直到reader.Read()== false为止。
eg : 例如:
DataReader data_reader= MySqlCommand.ExecuteReader( );
while(data_reader.Read())
{
...
}
I Change your GetItem method like this : 我这样更改您的GetItem方法:
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? 您的存储过程是返回ID的一行还是返回多行? 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.
如果存储过程每次调用返回一条记录,则需要遍历aspx页中的id。 If the stored procedure returns multiple rows, then you can use and sqlDataReader instead of the ExecuteScalar call.
如果存储过程返回多行,则可以使用和sqlDataReader代替ExecuteScalar调用。 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.
但是,您仍然必须修改aspx页面以处理集合。
你可以使用一个SqlDataReader的 ,或SqlDataAdapter的填写一个DataSet
,虽然对于web表单,你可能会更好地通过完全由使用的方式分离您的网页数据访问服务的ObjectDataSource 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.