ExecuteScalar SQL Server does not store an integer? C #

0

Good afternoon, I would like to know what I'm doing wrong because when I save the data through an insert, I keep the primary key with a very large value (it seems to be decimal) the table has the primary key as identity so that should be saved 1,2,3 .... etc.

but when doing the insert in the primary 1004,1005,1006 is saved every new record inserted .. and I want to occupy this id generated in another table at the same time so I do not want to have problems with that value, I prefer it to be Save from 1 onwards, this is my code:

public static bool crearPlanificacion(Planificacion recibo)
{
    var query = "INSERT INTO ActividadPlanificada (idActividadGenerica, idSucursalEmpresa,anio, mes,descripcionActividadPlanificada) VALUES" +
        "(@idActividadGenerica,@idSucursalEmpresa,@anio, @mes,@descripcionActividadPlanificada);SELECT SCOPE_IDENTITY(); ";

    SqlConnection connection = new SqlConnection(conexionString);

    try
    {
        connection.Open();
        //TANTAS VECES COMO idActividadGenerica VENGAN DESDE EL CLIENTE

            //Tabla ActividadPlanificada
            SqlCommand command = new SqlCommand(query, connection);
            command.Parameters.Add(new SqlParameter("@idActividadGenerica", SqlDbType.Int)).Value = recibo.idActividadGenerica; //desde array json
            command.Parameters.Add(new SqlParameter("@idSucursalEmpresa", SqlDbType.Int)).Value = recibo.idSucursalEmpresa;
            command.Parameters.Add(new SqlParameter("@anio", SqlDbType.Int)).Value = recibo.anio;
            command.Parameters.Add(new SqlParameter("@mes", SqlDbType.Int)).Value = recibo.mes;
            command.Parameters.Add(new SqlParameter("@descripcionActividadPlanificada", SqlDbType.Text)).Value = recibo.descripcionActividadPlanificada;

            //Ejecutamos la consulta y obtenemos la id de la actividadPlanificada para insertarla en las otras tablas
            Int32 pkActividadPlanificada = Convert.ToInt32(command.ExecuteScalar());

            //deseo ocupar esta variable para insertarla en otra tabla al mismo tiempo "pkActividadPlanificada"


        connection.Close();                
        return true;                
    }
    catch (Exception)
    {
        return false;
    }
}
    
asked by Kako 09.11.2017 в 20:15
source

0 answers