How to convert from string to varbinary?

0

I am working in the c # language and I would like to know how to convert a string data to varbinary since I am doing a query to a database (SQL Server) and it returns the data in string and one of those data I need to assign it to a variable that is in varbinary. I bring the data through a Datarow for the same if they know another way to bring data from a database with their source data and not to return them in string. Same use this conversion but it does not return what I want "user.foto = Encoding.UTF8.GetBytes (row [" photo "]. ToString ());" where "user.photo" is my variable varbinary and my row [ "Photo"] the variable string, I leave the code so that you can see

    public usuarioBO obtenerperfil()
    {
        ConexionDAO conexion = new ConexionDAO();
        var usuario = new usuarioBO();
        string strbuscar = string.Format("select * from Usuario where ID='1006';");
        DataTable dats = conexion.ejercutarsentrenciasdatable(strbuscar);
        DataRow row = dats.Rows[0];
        usuario.correo = row["Correo"].ToString();
        usuario.nombre = row["Nombre"].ToString();
        usuario.apellido = row["Apellido"].ToString();
        usuario.sexo = row["sexo"].ToString();
        usuario.correo = row["Correo"].ToString();
        usuario.telefono = row["Telefono"].ToString();
        usuario.fecha = Convert.ToDateTime(row["Fecha"].ToString());
        usuario.foto = Encoding.UTF8.GetBytes(row["foto"].ToString());


        return usuario;
    }
    
asked by NecroAny 19.11.2017 в 02:14
source

1 answer

0

varbinary is a byte[] so you only need to convert the result to byte [] to get the bytes of the column like this:

usuario.foto = (byte[])row["foto"];
    
answered by 19.11.2017 / 02:37
source