微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从收到的数据存储过程中填充自定义C#对象

public class User
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

public class Address
{
    public string City { get; set; }
    public string Country { get; set; }
}


/*
 * There are 2 c# objects i have shown 
 * There is a stored procedure in my application which
 * returns data for both objects simultaneously 
 * eg 
 * select FirstName,LasteName from Users where something="xyz"
 * select City,Country from Locations where something="xyz"
 * 
 * both queries are run by single procedure 
 * Now how can i fill both objects with from that stored procedure in asp.net using c#
*/

解决方法

使用ADO.NET,在使用参数执行SP的sqlCommand对象上打开sqlDataReader.使用sqlDataReader.NextResult方法获取第二个结果集.

基本上:

sqlConnection cn = new sqlConnection("<ConnectionString>");
cn.open();

sqlCommand Cmd = new sqlCommand("<StoredProcedureName>",cn);
Cmd.CommandType = System.Data.CommandType.StoredProcedure;

sqlDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);

while ( dr.Read() ) {
    // populate your first object
}

dr.NextResult();

while ( dr.Read() ) {
    // populate your second object
}

dr.Close();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐