joaomago Posted July 25, 2013 at 02:07 PM Report #519932 Posted July 25, 2013 at 02:07 PM Boas pessoal, Estou com um problema ao tentar ler um ficheiro em binário. O campo na base dados está em varbinary e está preenchido, mas ao tentar ler dá-me erro .. O código da minha classe está aqui: public static ArrayList GetAnexo(string id) { SqlCommand cmd = null; SqlConnection cn = null; SqlDataReader dr = null; ArrayList resultado = null; try { cn = new SqlConnection(dbloading); cn.Open(); cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = @"SELECT TOP 10000 [identificacaoEntidade].[iD] ,[Anexos] FROM [C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\IDT.mdf].[dbo].[identificacaoEntidade] WHERE [identificacaoEntidade].[iD]='" + id + "' "; cmd.ExecuteNonQuery(); dr = cmd.ExecuteReader(); if (dr.HasRows) { // ver se a bd devolveu registos resultado = new ArrayList(); while (dr.Read()) {// enquanto houver linhas para ler Entidades b = new Entidades(); if (!dr.IsDBNull(0)) { b.ID = dr.GetInt32(0); } if (!dr.IsDBNull(1)) { b.Anexos = dr.GetByte(1); } resultado.Add(b); } } } catch (Exception ex) { string erro = ex.StackTrace; } finally { if (dr != null) { dr.Close(); } if (cn != null && cn.State != ConnectionState.Closed) { cn.Close(); } } return resultado; } O erro é o seguinte: Acontece quando tenta ler o anexo, alguém me pode ajudar?
bioshock Posted July 25, 2013 at 04:03 PM Report #519959 Posted July 25, 2013 at 04:03 PM http://www.portugal-a-programar.pt/topic/61397-anexar-ficheiro/ Procura pelas minhas respostas.
joaomago Posted July 26, 2013 at 10:12 AM Author Report #520051 Posted July 26, 2013 at 10:12 AM Já li e reli e não entendo, além de já ter experimentado e não funciona
sYnced Posted July 26, 2013 at 10:43 AM Report #520055 Posted July 26, 2013 at 10:43 AM Uma pergunta é imagem que estás a por ? mysql ?
joaomago Posted July 26, 2013 at 01:37 PM Author Report #520073 Posted July 26, 2013 at 01:37 PM não, nem é imagem nem mysql ... ficheiros (.docx ; .pdf ; .txt ) e sql server
nelsonr Posted July 26, 2013 at 01:44 PM Report #520076 Posted July 26, 2013 at 01:44 PM Então tens um campo varbinary e estás a ler com getbyte? Experimentaste assim, como o bioshok disse? https://www.portugal-a-programar.pt/topic/61397-anexar-ficheiro/?do=findComment?comment=515742
joaomago Posted July 26, 2013 at 02:42 PM Author Report #520092 Posted July 26, 2013 at 02:42 PM Sim, pois o conteudo do campo são bytes, ou devo outro tipo? Já experimentei um processo identico, e não deu
brunobola Posted September 5, 2013 at 11:15 PM Report #523572 Posted September 5, 2013 at 11:15 PM Tens N maneiras de fazer experimenta como este exemplo: //Database connection code... cmd.CommandText = "SELECT FileData FROM Files WHERE ID = @ID"; cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1234; SqlDataReader sqlRead = cmd.ExecuteReader(); string fileName = "file.txt"; string fileDir = "C:\\Test\\"; string fileUrl = "/"; if (sqlRead.HasRows) { while(sqlRead.Read()) { byte[] fileData = (byte[]) sqlRead[0].Value; BinaryWriter fileCreate = new BinaryWriter(File.Open(fileDir + fileName, FileMode.Create)); fileCreate.Write(fileDate); fileCreate.Close(); HttpResponse.Redirect(fileUrl + fileName); } } sqlRead.Close();
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now