I am working on a visual C# program for image processing.I am trying to add image to sql database using Visual C# (Windows forms) and ADO.NET.
I have converted the image to binary form with filestream method but the image bytes are not getting saved in database. At the database image column, it says < Binary data > and no data is getting saved!
I have tried many methods for inserting( with and without stored procedures..etc) but always getting the same thing at database.
private void button6_Click(object sender, EventArgs e)
{
try
{
byte[] image = null;
pictureBox2.ImageLocation = textBox1.Text;
string filepath = textBox1.Text;
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
string sql = " INSERT INTO ImageTable(Image) VALUES(@Imgg)";
if (con.State != ConnectionState.Open)
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(new SqlParameter("@Imgg", image));
int x= cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(x.ToString() + "Image saved");
}
}
I am not getting error in code. the image got converted and entry is done in database but says < Binary Data > at the sql database.
Insert one image into SQL Server This table will have an integer (int) id and the image column named img. The data type that we are going to use to store images is the varbinary(max). The INSERT statement inserts the value 1 as the id and then inserts the image named 1. png from the folder img in the c drive.
1) Bring up SQL Server Management Studio. 2) Connect, and open the database you want to add the image to. 3) Expand the tables, and either add a new table with an appropriate index field, or right click teh table and select design. 4) Add a field called "myImage", and make its datatype "image".
How to view images stored in your database Start SQL Image Viewer and connect to your database. For SQL Server databases, tables containing blob columns will be highlighted in green in the list of database objects. Write the query to retrieve your images, and execute the query.
Press f5 key from keyword or from start button in Visual Studio. Now browse the image and click on upload button to upload the image in database. Now move to Sql Server and check image is uploaded. Also check the path folder where actual image is saving.
The selected file stream should be converted to byte array.
FileStream FS = new FileStream(filepath, FileMode.Open, FileAccess.Read); //create a file stream object associate to user selected file
byte[] img = new byte[FS.Length]; //create a byte array with size of user select file stream length
FS.Read(img, 0, Convert.ToInt32(FS.Length));//read user selected file stream in to byte array
Now this works fine. Database still shows < Binary data > but it could be converted back to image using memorystream method.
Thanks to all...
Try with something like this:
byte[] fileBytes=System.IO.File.ReadAllBytes("path to file");
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("insert into table(blob,filename) values (@blob,@name)");
command.Parameters.AddWithValue("blob", fileBytes);
command.Parameters.AddWithValue("name", "filename");
command.ExecuteNonQuery();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With