Set sql 'image' data

  • Thread starter Thread starter Chi Tang
  • Start date Start date
C

Chi Tang

Hi,

Does anybody know how to set the 'image' value in a sql database using
ado.net? For example, I have a captureBox on a windows form and I need to
put the image of this pictureBox into sql database but I tried both 'binary'
and 'image' data type on sql and neigher work. Thanks for any help,

CT
 
Hi!

If you need to save the file in your SQL Server then I would suggest you to
use the Image data type since it will be capaple of storing a lot bigger
files than the binary data type.

You did not supply very much info as to what you are doing in order to save
the data into the database so this will just be a suggestion as to how you
could do it.

First of all you need to get the binary data from the file, so just read the
data with a filestream or memorystream or something like that . Then you
just need to save the binary data to the image field in the database. This
should do the trick.

private void SaveDataToSqlServer(string fileName, byte[] fileData)
{
//The fileData is the binary information of the file you want to save
SqlConnection cn = new SqlConnection(MY_CONNECTION_STRING);

SqlCommand cmd = new SqlCommand("INSERT INTO MyFiles (FileName,
FileData) VALUES (@FileName, @FileData) WHERE FileId=@FileId", cn);
cmd.Parameters.Add("@FileName", fileName);
cmd.Parameters.Add("@FileData", fileData);

try
{
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
catch (Exception)
{
}
}

I hope this will help you out.

//Mikael
 
Wow! That is quick reply. Thanks for your help that I use your suggestion
to add the 'byte[]' image data and now my code is working. Thanks again,

CT

Mikael Gustavsson said:
Hi!

If you need to save the file in your SQL Server then I would suggest you to
use the Image data type since it will be capaple of storing a lot bigger
files than the binary data type.

You did not supply very much info as to what you are doing in order to save
the data into the database so this will just be a suggestion as to how you
could do it.

First of all you need to get the binary data from the file, so just read the
data with a filestream or memorystream or something like that . Then you
just need to save the binary data to the image field in the database. This
should do the trick.

private void SaveDataToSqlServer(string fileName, byte[] fileData)
{
//The fileData is the binary information of the file you want to save
SqlConnection cn = new SqlConnection(MY_CONNECTION_STRING);

SqlCommand cmd = new SqlCommand("INSERT INTO MyFiles (FileName,
FileData) VALUES (@FileName, @FileData) WHERE FileId=@FileId", cn);
cmd.Parameters.Add("@FileName", fileName);
cmd.Parameters.Add("@FileData", fileData);

try
{
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
catch (Exception)
{
}
}

I hope this will help you out.

//Mikael

Chi Tang said:
Hi,

Does anybody know how to set the 'image' value in a sql database using
ado.net? For example, I have a captureBox on a windows form and I need to
put the image of this pictureBox into sql database but I tried both 'binary'
and 'image' data type on sql and neigher work. Thanks for any help,

CT
 
Back
Top