クライアントインターフェイスガイド > TDV用TIBCO ADO .NET 2020データプロバイダー > ADO.NET (LINQ)の使用 > 保存されたプロシージャ
 
ストアドプロシージャ
次の例は、EntityFrameworkを使用してストアドプロシージャを呼び出す方法を示しています。
SQLを使用したストアドプロシージャの呼び出し
ストアドプロシージャを実行するには、EntityFrameworkコンテキストを介してSQLを直接実行します。ストアドプロシージャの結果をキャプチャするクラスを定義します。たとえば、以下に示すNameクラス。このクラスは、使用する出力列ごとにプロパティを定義する必要があります。このクラスを定義したら、EntityFrameworkコンテキストのExecuteStoreQueryメソッドを呼び出すことができます。
 
C#
 
public class SearchSuppliersOutput {
public string Name { get; set; }
// ... add other return values with matching data type
}
public class Program {
static void Main(string[] args) {
using (CompositeEntities context = new CompositeEntities())
{
//use the line below for versions earlier than EF 5:
//IEnumerable<SearchSuppliersOutput> result = context.ExecuteStoreQuery<SearchSuppliersOutput>("EXEC SearchSuppliers @Country = ?;",new CompositeParameter("Country", "US"));
//use the line below for EF 5 and 6:
IEnumerable<SearchSuppliersOutput> result = context.Database.SqlQuery<SearchSuppliersOutput>("EXEC SearchSuppliers @Country = ?", new CompositeParameter("Country", "US"));
List<SearchSuppliersOutput> resultList = result.ToList<SearchSuppliersOutput>();
foreach (SearchSuppliersOutput value in resultList) {
Console.WriteLine("Name: " + value.Name);
}
}
}
}
 
VB.NET
 
Public Class SearchSuppliersOutput
Public Property Name() As String
' ... add other return values with matching data type
End Class
Module Program
Sub Main
Using context As New CompositeEntities
'use the line below for versions earlier than EF 5:
'Dim result As IEnumerable(Of SearchSuppliersOutput) = context.ExecuteStoreQuery(Of SearchSuppliersOutput)("EXEC SearchSuppliers @Country = ?;", New CompositeParameter("Country", "US"))
'use the line below for EF 5 and 6:
Dim result As IEnumberable(Of SearchSuppliersOutput) = context.Database.SqlQuery(Of SearchSuppliersOutput)("EXEC SearchSuppliers @Country = ?;", New CompositeParameter("Country", "US"))
Dim resultList As List(Of SearchSuppliersOutput) = result.ToList()
For Each value As SearchSuppliersOutput In resultList
Console.WriteLine("Name: " + value.Name)
Next
End Using
End Sub
End Module
You can avoid using CompositeParameter by passing in the parameters directly, as shown in the following example:
 
"EXEC SearchSuppliers @Country=US, ..."