c# - 在集合中找不到参数 'param_name'

标签 c# mysql asp.net parameters

这是我第一次尝试使用 ADO.NET for MySQL,我不知道发生了什么。我在尝试定义的第一个参数上不断收到错误在集合中找不到参数“_name”

我尝试过重新排序,但无论我用哪种方式,MySQL 总是 catch 第一行。

mySqlCommand = mySqlConnect.CreateCommand();
mySqlCommand.CommandText = "SELECT MajorEquipment.EquipmentNumber, MajorEquipment.EquipmentType, PlantAreaCodes.AreaCode " +
  "FROM MajorEquipment e INNER JOIN PlantAreaCodes a " +
  "ON e.PACId = a.PACId WHERE " +
  "(@EquipmentType IS NULL OR e.EquipmentType = @EquipmentType) " +
  "AND (@EquipmentNumber IS NULL OR e.EquipmentNumber = @EquipmentNumber) " +
  "AND (@AreaCode IS NULL OR a.AreaCode = @AreaCode)";

// The First line shows the error. I have tried @EquipmentType, 
// @EquipmentNumber, and @AreaCode in the first line.

mySqlCommand.Parameters["@EquipmentType"].Value = (string.IsNullOrEmpty(MeModel.EquipmentType) ? (object)DBNull.Value : MeModel.EquipmentType);
mySqlCommand.Parameters["@EquipmentNumber"].Value = (string.IsNullOrEmpty(MeModel.EquipmentNumber) ? (object)DBNull.Value : MeModel.EquipmentNumber);
mySqlCommand.Parameters["@AreaCode"].Value = (string.IsNullOrEmpty(MeModel.Location) ? (object)DBNull.Value : MeModel.Location);

mySqlReader = mySqlCommand.ExecuteReader();

第一个参数显示错误,但我开始相信这可能与命令字符串有关。

我还尝试分别用 MajorEquipmentAreaCode 替换 ea

为什么会发生这种情况?

这是我的表格供引用:

+----------------+    +-----------------+
| PlantAreaCodes |    | MajorEquipment  |
|----------------|    |-----------------|
| PACId (PKEY)   |    | MEId (PKEY)     |
| AreaCode       |    | EquipmentType   |
| AreaName       |    | PACId (FKEY)    |
| Comments       |    | EquipmentNumber |
+----------------+    +-----------------+

最佳答案

您需要先将参数添加到集合中,然后再设置其值或使用快捷方式 添加值

mySqlCommand.Parameters.AddWithValue("@EquipmentType", (string.IsNullOrEmpty(MeModel.EquipmentType) ? (object)DBNull.Value : MeModel.EquipmentType));
mySqlCommand.Parameters.AddWithValue("@EquipmentNumber", (string.IsNullOrEmpty(MeModel.EquipmentNumber) ? (object)DBNull.Value : MeModel.EquipmentNumber));
mySqlCommand.Parameters.AddWithValue("@AreaCode", (string.IsNullOrEmpty(MeModel.Location) ? (object)DBNull.Value : MeModel.Location));

关于c# - 在集合中找不到参数 'param_name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22339300/

相关文章:

c# - 一次只选择一个项目的多个 WPF 列表框

c# - 将简单注入(inject)器与 SignalR 结合使用

mysql - 为什么这个 SELECT 查询没有返回我期望的结果?

mysql - 删除/删除的 WordPress 小部件会永远丢失吗?

asp.net - 避免编码到我的 ASP.NET 应用程序中以使其在共享主机上的中等信任下运行的最常见、典型的事情是什么?

c# - 用于检查日月年日期的 C# 函数

c# - 使用下级用户名检索用户主体对象

c# - 两个查询,一个 View

php - 通过同一页面上的短信通知用户其密码已更改(使用 PHP 或 AJAX?)

javascript - 如何使用 javascript 删除和撤消删除 gridview 行?