Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Wesley Silva

Inserir imagem no banco

Recommended Posts

E bem fácil cara..aki no caso estou usando o C# express para inserir no SqlServerprimeiro crio o método para conversão de string(caminho da foto) para binario : private byte[] GetPhoto(string filePath) { FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);// O objeto stream recebe como parametro o caminho do arquivo , logo após abre e lê o arquivo. BinaryReader reader = new BinaryReader(stream); byte[] photo = reader.ReadBytes((int)stream.Length); reader.Close(); stream.Close(); return photo; } Depois chamo de volta com o valor convertido : Byte[] photo = GetPhoto(Foto);e por fim inseri no bancoSqlCommand comandoSQL = new SqlCommand("INSERT INTO " + NomeTabela + " ( Codigo, Foto ) VALUES (@Codigo, @Foto )", Connection); comandoSQL.Parameters.Add("@Codigo", SqlDbType.Int, 20).Value = Codigo; comandoSQL.Parameters.Add("@Foto", SqlDbType.Image, photo.Length).Value = photo; Connection.Open(); comandoSQL.ExecuteNonQuery(); Connection.Close();Manda uma msg se naum funcionarflows

Compartilhar este post


Link para o post
Compartilhar em outros sites

niguerra,

estou usando o seu código mas esta dando erro o seguinte erro:

 

CODE
System.IO.DirectoryNotFoundException was unhandled by user code

Message="Could not find a part of the path 'C:\\Documents and Settings\\jlocately\\Desktop\\CONTROLE DE ACESSO\\Projeto ControleAcesso\\ProjetoFinComen\\ControleAcesso\\~\\Imagens_Visitantes\\nova.jpg'."

Source="mscorlib"

StackTrace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)

at Default2.GetPhoto(String filePath) in c:\Documents and Settings\jlocately\Desktop\CONTROLE DE ACESSO\Projeto ControleAcesso\ProjetoFinComen\ControleAcesso\Acervo.Presentation\Cadastros\RegistroEntrada.aspx.cs:line 79

at Default2.btnSalvar_Click(Object sender, EventArgs e) in c:\Documents and Settings\jlocately\Desktop\CONTROLE DE ACESSO\Projeto ControleAcesso\ProjetoFinComen\ControleAcesso\Acervo.Presentation\Cadastros\RegistroEntrada.aspx.cs:line 110

at System.Web.UI.WebControls.Button.OnClick(EventArgs e)

at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBa

ckEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

InnerException:

parece estar dizendo que a imagem não foi encotrada....ta muito estranho.

 

no banco de dados o campo que guarda a imagem eu defini com VARBINARY(MAX), é isso msm ou seria outro tipo?

 

vlw

Compartilhar este post


Link para o post
Compartilhar em outros sites

niguerra,

estou usando o seu código mas esta dando erro o seguinte erro:

 

CODE
System.IO.DirectoryNotFoundException was unhandled by user code

Message="Could not find a part of the path 'C:\\Documents and Settings\\jlocately\\Desktop\\CONTROLE DE ACESSO\\Projeto ControleAcesso\\ProjetoFinComen\\ControleAcesso\\~\\Imagens_Visitantes\\nova.jpg'."

Source="mscorlib"

StackTrace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)

at Default2.GetPhoto(String filePath) in c:\Documents and Settings\jlocately\Desktop\CONTROLE DE ACESSO\Projeto ControleAcesso\ProjetoFinComen\ControleAcesso\Acervo.Presentation\Cadastros\RegistroEntrada.aspx.cs:line 79

at Default2.btnSalvar_Click(Object sender, EventArgs e) in c:\Documents and Settings\jlocately\Desktop\CONTROLE DE ACESSO\Projeto ControleAcesso\ProjetoFinComen\ControleAcesso\Acervo.Presentation\Cadastros\RegistroEntrada.aspx.cs:line 110

at System.Web.UI.WebControls.Button.OnClick(EventArgs e)

at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBa

ckEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

InnerException:

parece estar dizendo que a imagem não foi encotrada....ta muito estranho.

 

no banco de dados o campo que guarda a imagem eu defini com VARBINARY(MAX), é isso msm ou seria outro tipo?

 

vlw

 

Amigo, coloque a sua dúvida no fórum correspondente a linguagem que você esta precisando de ajuda, como aqui é o fórum de Delphi, é pouco provável que alguém consiga lhe ajudar...

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.