Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Data access not allowed in current context?

Author  Topic 

tech_1
Posting Yak Master

129 Posts

Posted - 2012-04-12 : 11:27:17
Hi. totally new to the SQL CLR but extremely happy with the progress I am making.

I am trying to create a UDF. However everytime I try to execute the CLR UDF I get this error:

quote:

Msg 6260, Level 16, State 1, Line 2
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidOperationException: Data access is not allowed in this context. Either the context is a function or method not marked with DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow method of a Table Valued Function, or is a UDT validation method.
System.InvalidOperationException:
at System.Data.SqlServer.Internal.ClrLevelContext.CheckSqlAccessReturnCode(SqlAccessApiReturnCode eRc)
at System.Data.SqlServer.Internal.ClrLevelContext.AllocateNativeRequest(SmiEventSink sink, UrtCacheOptions ucoOption, Char* pwchSql, Int32 cwchSql, UrtCommandType uctType, UInt64 cbMaxSize, UInt32 cFields, UrtNativeRequest** ppNativeRequest, SqlTaskMem& stmMemory)
at Microsoft.SqlServer.Server.InProcRecordBuffer..ctor(ClrLevelContext clrlvlctxt, SmiExtendedMetaData[] columnMetaData, SmiEventSink eventSink)
at System.Data.SqlServer.Internal.ClrLevelContext.CreateRecordBuffer(SmiExtendedMetaData[] rgsmdDefinition, SmiEventSink eventSink)
at Microsoft.SqlServer.Server.SqlDataRecord..ctor(SqlMetaData[] metaData)
at DDSQLCLRWrapper.DegreeDaysSQLWrapper.<GetDegreesDayData>d__0.MoveNext()




Not sure where to go from here? The assembly is marked as EXTERNAL_ACCESS and ive also tried UNSAFE but but result in the same behavior.

code:

quote:

public static void FillDegreeDayRow(object item, out SqlString date, out SqlString value)
{
KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)item;
date = new SqlString(kvp.Key);
value = new SqlString(kvp.Value);
}



[SqlProcedure]
[SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read, FillRowMethodName = "FillDegreeDayRow", TableDefinition = "date nvarchar(200) null, value nvarchar(200) null")]
public static IEnumerable GetDegreesDayData(......)
{
...
...

DatedDataSet hddData = GetWebResponse(....);

SqlMetaData dateMeta = new SqlMetaData("Date", System.Data.SqlDbType.VarChar, 20);
SqlMetaData dateValue = new SqlMetaData("Value", System.Data.SqlDbType.VarChar, 20);
SqlDataRecord dataRecord = new SqlDataRecord(new[] { dateMeta, dateValue });


for (int counter = 0; counter < hddData.Values.Count; counter++)
{
yield return new KeyValuePair<string, string>(hddData.Values[counter].FirstDay.ToString(), hddData.Values[counter].Value.ToString());
}
}




I tried obtaining all the data, put it into a local collection then iterate through that in the same manner but still get the error.

if however I just do a return of the collection, I get the result displayed but only the single result, not the multiple results that exist.

tech_1
Posting Yak Master

129 Posts

Posted - 2012-04-12 : 12:34:17
fixed!

silly mistake on my part. I needed to remove the SqlMetaData and SqlDataRecord and all is now working.
Go to Top of Page
   

- Advertisement -