SQL CLR Stored Procedure Output

there is a simple class User and List of its objects

  public class User
{
public int ID;
public string UserName;
public string UserPassword;
}
...
List userList = new List();
   

Can I make this list of User objects as a result of executing the SLR CLR stored procedure? for example i want to get this

 
ID   UserName  UserPassword
1    Ted       SomePassword
2    Sam       Password2
3    Bill      dsdsd


[SqlProcedure]
public static void GetAllocations()
{
    // what here ??
}

PS Please do not advise me to use Sql functions. This does not suit me because it does not support output parameters

PS2 I will be very grateful for any help!

+3
source share
1 answer

Try creating a virtual table with SqlDataRecord and send it through the Pipe property of the SqlContext object:

[SqlProcedure]
public static void GetAllocations()
{
    // define table structure
    SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
        new SqlMetaData("ID", SqlDbType.Int),
        new SqlMetaData("UserName", SqlDbType.VarChar),
        new SqlMetaData("UserPassword", SqlDbType.VarChar),
    });

    // start sending and tell the pipe to use the created record
    SqlContext.Pipe.SendResultsStart(rec);
    {
        // send items step by step
        foreach (User user in GetUsers())
        {
            int id = user.ID;
            string userName = user.UserName;
            string userPassword = user.UserPassword;

            // set values
            rec.SetSqlInt32(0, id);
            rec.SetSqlString(1, userName);
            rec.SetSqlString(2, userPassword);

            // send new record/row
            SqlContext.Pipe.SendResultsRow(rec);
        }
    }
    SqlContext.Pipe.SendResultsEnd();    // finish sending
}
+3
source

Source: https://habr.com/ru/post/1782586/


All Articles