简体   繁体   English

SQL CLR存储过程输出

[英]SQL CLR stored procedure output

there is a simple class called User and List of its objects 有一个名为User的简单类和它的对象列表

 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 result of execution SLQ CLR stored procedure ? 我可以将这个User对象列表作为执行SLQ CLR存储过程的结果吗? eg 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 advice me to use Sql functions. PS请不要建议我使用Sql函数。 It does not suit me because it does not support output parameters 它不适合我,因为它不支持输出参数

PS2 i will be very appreciated for any help ! PS2我将非常感谢任何帮助!

Try to create a virtual table with SqlDataRecord and send it over the Pipe property of SqlContext object: 尝试使用SqlDataRecord创建一个虚拟表,并通过SqlContext对象的Pipe属性发送它:

[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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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