Jayson
Jayson

Reputation: 129

How to modify the DbType value for object SqlParameter type

I have a variable define like thisobject[] parameters = new object[0]; After run some code it will store some SqlParameter value. enter image description here

The problem is when i expand the value i wish to change the DbType to AnsiString if the data type is string only. Show in below picture. If the value is int, it will not modify the data type. enter image description here

So how to iterate the parameters in order to change it? I have try

foreach(var item in parameters)
            {
                item.DbType 
            }

but is getting error.

Upvotes: 0

Views: 1651

Answers (1)

Best Pactice of Collection of SqlParameters is to create the List and after the execution of command the SqlParameters will have the proper Value of the Sql Query.

So You can create a List of SqlParameters and you can handle it after the execution.

Examples of creating Sql Parameters:

cmd.Parameters.Add("FromID", SqlDbType.Int).Value = FromID;
cmd.Parameters.Add("ToID", SqlDbType.Int).Value = ToID;
cmd.Parameters.Add("User", SqlDbType.VarChar, 255).Value = User;
new SqlParameter("Name",[Value]){ SqlDbType = SqlDbType.VarChar, Size = 1000 }

Exaple of Insert List of Sql Parameters in SqlCommand

private Dictionary<string, SqlParameter> RunSqlStringCommand(string sqlString, List<SqlParameter> sqlParameters)
        {
            try
            {
                using (Database.Connection)
                {
                    Database.Connection.Open();
                    var cmd = Database.Connection.CreateCommand();
                    cmd.CommandText = sqlString;
                    cmd.CommandType = CommandType.StoredProcedure;
                    foreach (var item in sqlParameters) cmd.Parameters.Add(item.Value);
                    cmd.ExecuteReader();
                    Database.Connection.Close();
                    return sqlParameters;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

After execution the Sql Parameters if they have the output feature, they have the inserted value from procedure.

Upvotes: 2

Related Questions