Retrieve data from stored procedure which has multiple result sets

后端 未结 9 514
一个人的身影
一个人的身影 2020-11-30 08:47

Given a stored procedure in SQL Server which has multiple select statements, is there a way to work with those results separately while calling the procedure?

9条回答
  •  北海茫月
    2020-11-30 09:06

    String myConnString  = "User ID="username";password="password";Initial Catalog=pubs;Data Source=Server";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand();
    SqlDataReader myReader ;
    
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Connection = myConnection;
    myCommand.CommandText = "MyProc";
    
    try
    {
        myConnection.Open();
        myReader = myCommand.ExecuteReader();
    
        while (myReader.Read())
        {
            //Write logic to process data for the first result.   
            }
    
        myReader.NextResult();
        while (myReader.Read())
        {
            //Write logic to process data for the second result.
        }
    }
    

提交回复
热议问题