.NET、SQL Server、ストアドプロシージャで「Procedure expects parameter which was not supplied」エラーを解決する
.NET、SQL Server、ストアドプロシージャにおける「Procedure expects parameter which was not supplied」エラーの解説
このエラーは、C#などの.NET言語からSQL Serverのストアドプロシージャを実行しようとするときに、必要なパラメータが渡されていない場合に発生します。
原因
このエラーが発生する主な原因は、以下の2つです。
- ストアドプロシージャのパラメータとC#コードのパラメータが一致していない
- C#コードで必要なパラメータが渡されていない
解決方法
以下の手順で問題を解決できます。
-
- ストアドプロシージャのパラメータ名とデータ型が、C#コードで渡しているパラメータ名とデータ型と一致していることを確認します。
- パラメータの順序も一致していることを確認します。
-
C#コードで必要なパラメータを渡していることを確認する
- すべての必須パラメータに値が渡されていることを確認します。
- オプションパラメータに値を渡す場合は、
null
ではなく適切な値を渡します。
- 使用している.NET言語とバージョン
- 使用しているSQL Serverのバージョン
- ストアドプロシージャの定義
- C#コード
using System;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
// 接続文字列
string connectionString = "Data Source=localhost;Initial Catalog=Test;Integrated Security=True";
// ストアドプロシージャ名
string storedProcedureName = "GetProducts";
// パラメータ
SqlParameter parameter1 = new SqlParameter("@categoryId", 1);
SqlParameter parameter2 = new SqlParameter("@minPrice", 100);
// SqlCommandオブジェクトを作成
using (SqlCommand command = new SqlCommand(storedProcedureName, new SqlConnection(connectionString)))
{
// コマンドタイプをストアドプロシージャに設定
command.CommandType = CommandType.StoredProcedure;
// パラメータを追加
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);
// コマンドを実行
using (SqlDataReader reader = command.ExecuteReader())
{
// 結果処理
while (reader.Read())
{
Console.WriteLine("{0} - {1}", reader["ProductName"], reader["Price"]);
}
}
}
}
}
このコードは、GetProducts
というストアドプロシージャを実行し、categoryId
が1でminPrice
が100以上の製品情報を取得します。
注意事項
このコードはサンプルであり、実際の環境に合わせて変更する必要があります。
C#からSQL Serverのストアドプロシージャを実行する他の方法
SqlConnection
クラスの ExecuteStoredProcedure
メソッドを使用すると、ストアドプロシージャを実行できます。
using System;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
// 接続文字列
string connectionString = "Data Source=localhost;Initial Catalog=Test;Integrated Security=True";
// ストアドプロシージャ名
string storedProcedureName = "GetProducts";
// パラメータ
SqlParameter parameter1 = new SqlParameter("@categoryId", 1);
SqlParameter parameter2 = new SqlParameter("@minPrice", 100);
// SqlConnectionオブジェクトを作成
using (SqlConnection connection = new SqlConnection(connectionString))
{
// コマンドオブジェクトを作成
using (SqlCommand command = new SqlCommand(storedProcedureName, connection))
{
// コマンドタイプをストアドプロシージャに設定
command.CommandType = CommandType.StoredProcedure;
// パラメータを追加
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);
// コマンドを実行
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
このコードは、GetProducts
というストアドプロシージャを実行します。
using System;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
// 接続文字列
string connectionString = "Data Source=localhost;Initial Catalog=Test;Integrated Security=True";
// ストアドプロシージャ名
string storedProcedureName = "GetProducts";
// パラメータ
SqlParameter parameter1 = new SqlParameter("@categoryId", 1);
SqlParameter parameter2 = new SqlParameter("@minPrice", 100);
// SqlConnectionオブジェクトを作成
using (SqlConnection connection = new SqlConnection(connectionString))
{
// コマンドオブジェクトを作成
using (SqlCommand command = new SqlCommand(storedProcedureName, connection))
{
// コマンドタイプをストアドプロシージャに設定
command.CommandType = CommandType.StoredProcedure;
// パラメータを追加
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);
// コマンドを実行
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
// 結果処理
while (reader.Read())
{
Console.WriteLine("{0} - {1}", reader["ProductName"], reader["Price"]);
}
}
}
}
}
}
Entity Frameworkを使用すると、C#コードからストアドプロシージャを簡単に実行できます。
using System;
using System.Data.Entity;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// DbContextオブジェクトを作成
using (var context = new MyContext())
{
// ストアドプロシージャを実行
var products = context.Database.SqlQuery<Product>("GetProducts @categoryId, @minPrice", 1, 100).ToList();
// 結果処理
foreach (var product in products)
{
Console.WriteLine("{0} - {1}", product.Name, product.Price);
}
}
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class MyContext : DbContext
{
public MyContext() : base("Test")
{
}
public DbSet<Product> Products { get; set; }
}
.net sql-server stored-procedures