“SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects”

后端 未结 8 1920
暗喜
暗喜 2021-01-04 07:23

I keep getting the exception

The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects

, while execut

相关标签:
8条回答
  • 2021-01-04 07:37

    I had been getting the same error and had to use AddWithValue like this...

    cmd.Parameters.AddWithValue(@columnToUpdate, newValue);
    cmd.Parameters.AddWithValue(@conditionalColumn, conditionalValue);
    
    0 讨论(0)
  • 2021-01-04 07:37

    I tried all the above-mentioned steps but nothing helps.

    Finally found and fixed it by changing the namespace

    using System.Data.SqlClient to using Microsoft.Data.SqlClient

    Arshad

    0 讨论(0)
  • 2021-01-04 07:48

    I ran into the same issue, but mine was doing an object[2] = object[1] as SqlParameters, similar to what was being tried.

    Just to add to the thread, I have this simple object array of SqlParameters added from a method like this,

        private SqlParameter GetGenericParamsObject(string name, object data)
        {
            return new SqlParameter(name, SetSqlDataType(data.GetType().ToString())) { Direction = Input, Value = data };
        }
    

    Where there is a simple switch for the SetSqlDataType(), i.e. SqlDbType.Int is one of the return types to set it.

    Then I run

        private static void ExecuteSqlCommand(DbContext dbContext, string sql, params object[] sqlParameters)
        {
            try
            {
                if (dbContext.Database.Connection.State == ConnectionState.Closed)
                    dbContext.Database.Connection.Open();
                var cmd = dbContext.Database.Connection.CreateCommand();
                cmd.CommandText = sql;
                foreach (var param in sqlParameters)
                    cmd.Parameters.Add(param);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    

    This helps with casting to the proper data type and moves the param name, datatype and object out of the command method for easier debugging and then just adding the entire SqlParameter to the cmd.Parameters.

    0 讨论(0)
  • 2021-01-04 07:48

    I ran into the same error when I was using

    cmd.Parameters.Add(pars)
    

    where pars was a an array of SqlParameter. The issue was that I was using the Add() function but I should've used AddRange() instead.

    cmd.Parameters.AddRange(pars)
    
    0 讨论(0)
  • 2021-01-04 07:49

    When you use Add method, you are trying to add a new parameter. What you want to do is to assign value. So change this:

    comm.Parameters.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
    

    to this:

    comm.Parameters["@author"].Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
    

    Similarly for the other parameters.

    0 讨论(0)
  • 2021-01-04 07:50

    For any future readers of this question - the examples deal with .Add(), but .Remove() will also throw this error if not used correctly. The proper way to use .Add(), as has been documented by others is :

    cmd.Parameters.AddWithValue("Key", "Value");
    

    The proper way to use .Remove() is:

    cmd.Parameters.Remove(command.Parameters["Key"]); 
    
    0 讨论(0)
提交回复
热议问题