Error calling a stored procedure in c #

0

I have a problem wanting to call a stored procedure in c #, the stored procedure is to look for a name of a client, I already try it with the select which works for me but wanting to use a stored procedure does not work for me someone could help me

It works for me this way, I'm using a textbox which when I put the client's name shows it to me in the datagridView, but wanting to do it with the stored procedure does not work for me

public DataTable Buscar(string descripcion)
        {
            connectionSql.Open();
         SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM CATALOGO WHERE NAMECLIENTE LIKE '%{0}%'",descripcion), connectionSql);
           SqlDataAdapter ad = new SqlDataAdapter(cmd);
            ds = new DataSet();
            ad.Fill(ds,"@NAME");
            connectionSql.Close();
            return ds.Tables["@NAME"];
        }

I did it this way but it marks me an error

public DataTable Buscar(string descripcion)
    {

        SqlCommand cmd = new SqlCommand("CALL SP_BUSCAR", connectionSql);
        cmd.CommandType = CommandType.StoredProcedure;
        connectionSql.Open();

       SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ds = new DataSet();
            ad.Fill(ds,"TABLA");//AQUI LO CAMBIE POR @NAME PERO NO FUNCIONA
//QUE ES EL WHERE DEL STORED PROCEDURE
                connectionSql.Close();
                return ds.Tables["TABLA"];
            }

DataTable dtresultado = new  DataTable();
        try
        {

            SqlCommand sqlcmd = new SqlCommand("CALL SP_BUSCAR",connectionSql);
            sqlcmd.CommandType = CommandType.StoredProcedure;

            connectionSql.Open();
            SqlDataAdapter sqlDat = new SqlDataAdapter(sqlcmd);
            sqlDat.Fill(dtresultado);
            sqlcmd.ExecuteNonQuery();
            connectionSql.Close();

        }

        catch (Exception ex)
        {
            dtresultado = null;
        }
        return dtresultado;
    
asked by Daniel 23.09.2017 в 18:42
source

1 answer

0

Omit the CALL instruction. On the other hand, if the method returns a DataTable type, it creates an instance of type DataTable, it is unnecessary to create an instance of DataSet type.

public DataTable Buscar(string descripcion)
    {
        using (SqlConnection conn = new SqlConnection("Cadena de conexión"))
        {
            SqlDataAdapter da = new SqlDataAdapter("SP_BUSCAR", conn);
            DataTable dt = new DataTable();

            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            //da.SelectCommand.Parameters.AddWithValue("@Descripcion", descripcion);

            da.Fill(dt);

            return dt;
        }
    }
    
answered by 23.09.2017 / 19:25
source