SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Data access not allowed in current context?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tech_1
Posting Yak Master

105 Posts

Posted - 04/12/2012 :  11:27:17  Show Profile  Reply with Quote
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.

Edited by - tech_1 on 04/12/2012 11:33:08

tech_1
Posting Yak Master

105 Posts

Posted - 04/12/2012 :  12:34:17  Show Profile  Reply with Quote
fixed!

silly mistake on my part. I needed to remove the SqlMetaData and SqlDataRecord and all is now working.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000