[Microsoft.SqlServer.Server.SqlProcedure]
public static void MyMethod()
{
string connectionString = "context connection=true";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlMetaData[] metaData = {
new SqlMetaData("Column1", System.Data.SqlDbType.NVarChar)
,new SqlMetaData("Column1", System.Data.SqlDbType.NVarChar)
};
SqlDataRecord record = new SqlDataRecord(metaData);
record.SetString(0,"hello world");
SqlContext.Pipe.SendResultsRow(record);
}
}
When I run the method in SQL
EXEC MyMethod
Error
Msg 6522, Level 16, State 1, Procedure MyMethod, Line 0 A .NET Framework error occurred during execution of user-defined routine or aggregate "MyMethod": System.ArgumentException: The dbType NVarChar is invalid for this constructor. System.ArgumentException: at Microsoft.SqlServer.Server.SqlMetaData.Construct(String name, SqlDbType dbType, Boolean useServerDefault, Boolean isUniqueKey, SortOrder columnSortOrder, Int32 sortOrdinal) at Microsoft.SqlServer.Server.SqlMetaData..ctor(String name, SqlDbType dbType) at WcfClrApps.MyNamespace.MyMethod()
How can a return a record that I create myself? I do not want to run any SQL. Project build is set for .NET 3.5. MSDN indicates 4.0 is not supported in SQL 2008 R2.
The problem is two fold. 1. Max length is required. 2. SendResultsStart()
/SendResultsEnd()
are required.
[Microsoft.SqlServer.Server.SqlProcedure]
public static void MyMethod()
{
string connectionString = "context connection=true";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlMetaData[] metaData = {
new SqlMetaData("Column1", System.Data.SqlDbType.NVarChar, 100)//Max length has to be specified
,new SqlMetaData("Column1", System.Data.SqlDbType.NVarChar, 100)//same story
};
SqlDataRecord record = new SqlDataRecord(metaData);
SqlContext.Pipe.SendResultsStart(record);//SendResultsStart must be called
//create a row and send it down the pipe
record.SetString(0,"hello world");
SqlContext.Pipe.SendResultsRow(record);
SqlContext.Pipe.SendResultsEnd();//End it out
}
}