有一个名为 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/