我的問題是非常相似的this問題。SQL CLR:流表值函數的結果
不過,我使用SQL Server 2005的Service Pack 2(SP2)(v9.0.3042)和解決方案張貼有沒有爲我工作。我試着使用兩個連接字符串。其中一個在我的代碼中被註釋掉了。
我知道我可以存儲在內存中的列表或ArrayList中所有的結果並返回。我已經成功完成了這一目標,但這不是目標。我們的目標是能夠在可用時傳輸結果。
這可能使用我的版本的SQL Server?
這裏是我的代碼: (注意參數實際上並沒有被目前使用的我這樣做了調試。)
public static class StoredProcs
{
[SqlFunction(
DataAccess = DataAccessKind.Read,
SystemDataAccess=SystemDataAccessKind.Read,
FillRowMethodName="FillBaseline",
TableDefinition = "[baseline_id] [int], [baseline_name] [nvarchar](256), [description] [nvarchar](max), [locked] [bit]"
)]
public static IEnumerable fnGetBaselineByID(SqlString projectName, SqlInt32 baselineID)
{
string connStr = "context connection=true";
//string connStr = "data source=.;initial catalog=DBName;integrated security=SSPI;enlist=false";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(String.Format(@"
SELECT *
FROM [DBName].[dbo].[Baseline] WITH (NOLOCK)
"), conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
yield return new Baseline(reader);
}
}
}
};
}
public static void FillBaseline(Object obj, out SqlInt32 id, out SqlString name, out SqlString description, out bool locked)
{
Baseline baseline = (Baseline)obj;
id = baseline.mID;
name = baseline.nName;
description = baseline.mDescription;
locked = baseline.mLocked;
}
}
這裏是我的SQL部署腳本的一部分:
CREATE ASSEMBLY [MyService_Stored_Procs]
FROM 'C:\temp\assemblyName.dll'
WITH PERMISSION_SET = SAFE
當我使用連接字符串「context connection = true」時,出現此錯誤:
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.
當我使用其他連接字符串我得到這個錯誤:
An error occurred while getting new row from user defined Table Valued Function : System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
廢話,我在浪費了半天的時間才發現這個問題,問題一直是「收益」。 – Patrick