Save photo to database and then display it

Asked

Viewed 86 times

0

I have an application to register students where will be inserted the photo, I want to save it and then display it, but I have a save button where I want to save all the inserted information plus the photo as shown below:

SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-SEFSOUP\SQLEXPRESS;Initial Catalog=DISPENSA;Integrated Security=True");
string sql = "INSERT INTO ALUNO(RM_ALUNO,FT_ALUNO,NM_ALUNO,SERIE_ALUNO,DTNASC_ALUNO, PERIODO, CURSO, RG_RESPON_ALUNO_1, NM_RESPON_ALUNO_1, TEL_RESPON_ALUNO_1,RG_RESPON_ALUNO_2, NM_RESPON_ALUNO_2, TEL_RESPON_ALUNO_2, RG_RESPON_ALUNO_3, NM_RESPON_ALUNO_3, TEL_RESPON_ALUNO_3, RG_RESPON_ALUNO_4, NM_RESPON_ALUNO_4, TEL_RESPON_ALUNO_4,RG_RESPON_ALUNO_5, NM_RESPON_ALUNO_5, TEL_RESPON_ALUNO_5) VALUES (@RM_ALUNO,@FT_ALUNO,@NM_ALUNO,@SERIE_ALUNO,@DTNASC_ALUNO,@PERIODO,@CURSO,@RG_RESPON_ALUNO_1,@NM_RESPON_ALUNO_1,@TEL_RESPON_ALUNO_1,@RG_RESPON_ALUNO_2,@NM_RESPON_ALUNO_2,@TEL_RESPON_ALUNO_2,@RG_RESPON_ALUNO_3,@NM_RESPON_ALUNO_3,@TEL_RESPON_ALUNO_3,@RG_RESPON_ALUNO_4,@NM_RESPON_ALUNO_4,@TEL_RESPON_ALUNO_4,@RG_RESPON_ALUNO_5,@NM_RESPON_ALUNO_5,@TEL_RESPON_ALUNO_5)";


//Cria uma objeto do tipo comando passando como parametro a string sql e a string de conexão
SqlCommand comando = new SqlCommand(sql, con);
//Adicionando o valor das textBox nos parametros do comando
comando.Parameters.Add(new SqlParameter("@RM_ALUNO", this.txtRm.Text));
comando.Parameters.Add(new SqlParameter("@FT_ALUNO", System.Data.SqlDbType.Binary));
comando.Parameters.Add(new SqlParameter("@NM_ALUNO", this.txtNomeAluno.Text));
comando.Parameters.Add(new SqlParameter("@SERIE_ALUNO", this.txtSerie.Text));
comando.Parameters.Add(new SqlParameter("@DTNASC_ALUNO", this.mtbDataNasct.Text));
comando.Parameters.Add(new SqlParameter("@PERIODO", this.txtPeriodo.Text));
comando.Parameters.Add(new SqlParameter("@CURSO", this.txtCurso.Text));
comando.Parameters.Add(new SqlParameter("@RG_RESPON_ALUNO_1", this.mtbRG.Text));
comando.Parameters.Add(new SqlParameter("@NM_RESPON_ALUNO_1", this.txtNomeResp.Text));
comando.Parameters.Add(new SqlParameter("@TEL_RESPON_ALUNO_1", this.mtbTelResp.Text));
comando.Parameters.Add(new SqlParameter("@RG_RESPON_ALUNO_2", this.mtbRG2.Text));
comando.Parameters.Add(new SqlParameter("@NM_RESPON_ALUNO_2", this.txtNomeResp1.Text));
comando.Parameters.Add(new SqlParameter("@TEL_RESPON_ALUNO_2", this.mtbTelResp2.Text));
comando.Parameters.Add(new SqlParameter("@RG_RESPON_ALUNO_3", this.mtbRG3.Text));
comando.Parameters.Add(new SqlParameter("@NM_RESPON_ALUNO_3", this.txtNomeResp2.Text));
comando.Parameters.Add(new SqlParameter("@TEL_RESPON_ALUNO_3", this.mtbTelResp3.Text));
comando.Parameters.Add(new SqlParameter("@RG_RESPON_ALUNO_4", this.mtbRG3.Text));
comando.Parameters.Add(new SqlParameter("@NM_RESPON_ALUNO_4", this.txtNomeResp3.Text));
comando.Parameters.Add(new SqlParameter("@TEL_RESPON_ALUNO_4", this.mtbTelResp4.Text));
comando.Parameters.Add(new SqlParameter("@RG_RESPON_ALUNO_5", this.mtbRG5.Text));
comando.Parameters.Add(new SqlParameter("@NM_RESPON_ALUNO_5", this.txtNomeResp4.Text));
comando.Parameters.Add(new SqlParameter("@TEL_RESPON_ALUNO_5", this.mtbTelResp5.Text));

I don’t know how to pass the photo parameter.

And the add photo button is like this:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    //Exibir a Imagem no formulário
    string nomeArquivo = openFileDialog1.FileName;
    Bitmap bmp = new Bitmap(nomeArquivo);
    pcbFoto.Image = bmp;

    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Bmp);
    byte[] foto = ms.ToArray();
}

I am using SQL Server 2008 and C# in Visual Studio.

  • remembering that I want to save only the image path in the bank

  • Take a look at this topic https://answall.com/a/43901/7152

  • I looked at the topic and used but is giving implicit conversion error and that is telling me that I have to use CONVERT

  • Note that in this answer is being used the image path, which is a string. You should be aware if you are trying to record the image path, or the image itself.

  • write the image path, in the bd is as varbinary, will be on account of that??

  • If you want to save only the image path, then the field in the database has to accept a string. Try changing to char or varchar in the database table and run a test.

Show 1 more comment

2 answers

0

Hello. Try changing the type of the Binary parameter to Image.

comando.Parameters.Add(new SqlParameter("@FT_ALUNO", SqlDbType.Image));

0

Thus:

comando.Parameters.Add(new SqlParameter("@NOME_CAMPO_IMAGEM", nomeArquivo));

But the variable string nomeArquivo must be declared globally to be viewed at the time of bank recording. And you also need to see if there is a string field in the bank table to receive this value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.