在集合中找不到参数"param_name" [英] Parameter 'param_name' not found in collection
问题描述
这是我第一次尝试将ADO.NET用于MySQL,但我不知道发生了什么.我一直在尝试定义的第一个参数上收到错误Parameter '_name' not found in collection
.
This is my first attempt at using ADO.NET for MySQL, and I have no idea what's going on. I keep getting the error Parameter '_name' not found in collection
on the first parameter I try to define.
我已经尝试过重新排序,但是无论我采用哪种方式,MySQL总是会赶上第一行.
I have tried reordering, but no matter which way I put it, MySQL always catches up of the first line.
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();
第一个参数显示错误,但是我开始相信这可能与Command字符串有关.
The first parameter shows the error, but I'm starting to believe that it may be something to do with the Command string.
我还尝试用MajorEquipment
和AreaCode
分别替换e
和a
.
I have also tried replacing e
and a
with MajorEquipment
and AreaCode
respectively.
为什么会这样?
这是我的表格供参考:
+----------------+ +-----------------+
| PlantAreaCodes | | MajorEquipment |
|----------------| |-----------------|
| PACId (PKEY) | | MEId (PKEY) |
| AreaCode | | EquipmentType |
| AreaName | | PACId (FKEY) |
| Comments | | EquipmentNumber |
+----------------+ +-----------------+
推荐答案
您需要先将参数添加到集合中,然后再设置其值或使用快捷方式 AddWithValue
You need to add the parameters to the collection before settings their values or use the shortcut AddWithValue
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));
这篇关于在集合中找不到参数"param_name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!