Parameter “@Name” not found in the collection

对着背影说爱祢 提交于 2019-12-24 21:24:19

问题


I have come across this solution recently as part of another problem I was having when trying to select from multiple tables which variable or null input on three fields. Here is the original post.

Unfortunately I am still a little unfamiliar with ADO.NET and I am getting this error when I run my code:

Parameter "@AreaName" not found in the collection

Here is the code for the query:

mySqlConnect.Open();

mySqlCommand = mySqlConnect.CreateCommand();
mySqlCommand.CommandText = "SELECT * FROM PlantAreaCodes WHERE (@AreaCode IS NULL OR AreaCode LIKE " + PModel.AreaCode + " OR @AreaName IS NULL OR LIKE " + PModel.AreaName + " OR @Comments IS NULL OR Comments LIKE " + PModel.Comments + ";";
mySqlCommand.Parameters["@AreaCode"].Value = (string.IsNullOrEmpty(Convert.ToString(PModel.AreaCode)) ? (object)DBNull.Value : PModel.AreaCode);
mySqlCommand.Parameters["@AreaName"].Value = (string.IsNullOrEmpty(PModel.AreaName) ? (object)DBNull.Value : PModel.AreaName);
mySqlCommand.Parameters["@Comments"].Value = (string.IsNullOrEmpty(PModel.Comments) ? (object)DBNull.Value : PModel.Comments);
mySqlReader = mySqlCommand.ExecuteReader();

The values passed in are:

PModel.AreaCode = 110;
AreaName = null;
Comments = null;

I have completed this once before, however I unfortunately lost a segment of my code recently and have had to rebuild.


回答1:


You need to add the parameters before specifying a value:

mySqlCommand.Parameters.Add("@AreaName", SqlDbType.VarChar);

mySqlCommand.Parameters["@AreaName"].Value =
    (string.IsNullOrEmpty(Convert.ToString(PModel.AreaName))
        ? (object)DBNull.Value : PModel.AreaName));

Consider using this alternate syntax:

mySqlCommand.Parameters.AddWithValue("@AreaName",
    (string.IsNullOrEmpty(Convert.ToString(PModel.AreaName))
        ? (object)DBNull.Value : PModel.AreaName));

You can find a brief example of each on the SqlCommand.Parameters Property page at MSDN.


Also, you've done something funky with the query string. This should fix it:

mySqlCommand.CommandText =
    "SELECT * FROM PlantAreaCodes WHERE (AreaCode IS NULL OR AreaCode LIKE @AreaCode OR AreaName IS NULL OR AreaName LIKE @AreaName OR Comments IS NULL OR Comments LIKE @Comment;";

To get the LIKEs to work correctly, you'll probably have to add % symbols around your parameter values when you're adding them:

var areaName = (string.IsNullOrEmpty(Convert.ToString(PModel.AreaName))
                   ? (object)DBNull.Value : PModel.AreaName)

mySqlCommand.Parameters.AddWithValue("@AreaName", "%" + areaName + "%");



回答2:


Have you added the parameters? Looks like you are just trying to set the value?

Try this.

mySqlCommand.Parameters.AddWithValue("@AreaCode", PModel.AreaCode == 0 ? (object)DBNull.Value : PModel.AreaCode.ToString());

You need to adjust your SQL text also.

mySqlCommand.CommandText = "SELECT * FROM PlantAreaCodes WHERE (@AreaCode IS NULL OR AreaCode LIKE AreaCode) AND (@AreaName IS NULL OR LIKE @AreaName) AND (@Comments IS NULL OR Comments LIKE @Comments);";


来源:https://stackoverflow.com/questions/23669569/parameter-name-not-found-in-the-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!