这是我第一次尝试使用 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();
第一个参数显示错误,但我开始相信这可能与命令字符串有关。
我还尝试分别用 MajorEquipment
和 AreaCode
替换 e
和 a
。
为什么会发生这种情况?
这是我的表格供引用:
+----------------+ +-----------------+
| 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/