Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Topik ini menunjukkan cara menjalankan kueri SQL Entitas yang memiliki parameter menggunakan objek EntityCommand.
Untuk menjalankan kode dalam contoh ini
Tambahkan AdventureWorks Sales Model ke proyek Anda dan konfigurasikan proyek Anda untuk menggunakan Entity Framework. Untuk informasi selengkapnya, lihat Cara: Menggunakan Wizard Model Data Entitas.
Di halaman kode untuk aplikasi Anda, tambahkan arahan berikut
using(Importsdi Visual Basic):using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.EntityClient; using System.Data.Metadata.Edm;Imports System.Collections.Generic Imports System.Collections Imports System.Data.Common Imports System.Data Imports System.IO Imports System.Data.SqlClient Imports System.Data.EntityClient Imports System.Data.Metadata.Edm
Contoh
Contoh berikut menunjukkan cara membuat string kueri dengan dua parameter. Ini kemudian membuat EntityCommand, menambahkan dua parameter ke koleksi EntityParameter dari EntityCommand itu, dan mengulangi koleksi Contact item.
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
// Create a query that takes two parameters.
string esqlQuery =
@"SELECT VALUE Contact FROM AdventureWorksEntities.Contacts
AS Contact WHERE Contact.LastName = @ln AND
Contact.FirstName = @fn";
using (EntityCommand cmd = new EntityCommand(esqlQuery, conn))
{
// Create two parameters and add them to
// the EntityCommand's Parameters collection
EntityParameter param1 = new EntityParameter();
param1.ParameterName = "ln";
param1.Value = "Adams";
EntityParameter param2 = new EntityParameter();
param2.ParameterName = "fn";
param2.Value = "Frances";
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
// Iterate through the collection of Contact items.
while (rdr.Read())
{
Console.WriteLine(rdr["FirstName"]);
Console.WriteLine(rdr["LastName"]);
}
}
}
conn.Close();
}
Using conn As New EntityConnection("name=AdventureWorksEntities")
conn.Open()
' Create a query that takes two parameters.
Dim esqlQuery As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
" AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"
Using cmd As New EntityCommand(esqlQuery, conn)
' Create two parameters and add them to
' the EntityCommand's Parameters collection
Dim param1 As New EntityParameter()
param1.ParameterName = "ln"
param1.Value = "Adams"
Dim param2 As New EntityParameter()
param2.ParameterName = "fn"
param2.Value = "Frances"
cmd.Parameters.Add(param1)
cmd.Parameters.Add(param2)
Using rdr As DbDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Iterate through the collection of Contact items.
While rdr.Read()
Console.WriteLine(rdr("FirstName"))
Console.WriteLine(rdr("LastName"))
End While
End Using
End Using
conn.Close()
End Using