SQL CLR 存储过程输出

标签 sql clr sqlclr

有一个名为 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 ? e.g. i want to get this

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


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

附言请不要建议我使用 Sql 函数。它不适合我,因为它不支持输出参数

P.S.2 如果有任何帮助,我将不胜感激!

最佳答案

尝试用 SqlDataRecord 创建一个虚拟表并通过 Pipe 发送SqlContext的属性(property)对象:

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

关于SQL CLR 存储过程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4554217/

相关文章:

php - 如何使用 SQL 选择查询获取特定数据?

mysql - 我可以根据多个连接创建一个查询并求和吗?

c# - 为 CLR 过程添加程序集

c# - 用户取消查询时如何回滚SQL CLR存储过程?

sql - 对 "super row"之后的行进行分组

sql - 在带括号的 PostgreSQL 中使用 LIKE

c# - CLR 检测到无效程序

.net - .Net 中触发垃圾收集的标准

mysql - 将记录从 MySQL 数据库导入 MS SQL 的最佳解决方案(每小时)

sql-server - 将用户定义的 CLR 函数的执行限制为 sysadmin 而不是 dbowner