问题
I am trying to store images in sql server with data type image
now the problem is getting stored into two rows, i am using FileUpload control to upload image, my code is as follows
byte[] imagedata = ImageUpload.FileBytes;
con.Open();
SqlCommand insertImageCmd = new SqlCommand("insert into Images(ImageName,Image) values (@name, @image)", con);
insertImageCmd.Parameters.AddWithValue("@name", imageNameTextBox.Text);
insertImageCmd.Parameters.AddWithValue("@image", imagedata );
insertImageCmd.ExecuteNonQuery();
con.Close();
is this the correct way store images? please help!
回答1:
If its getting stored as two rows, using the code you've posted, I think that you might somehow be double posting your page.
回答2:
Here is what I would do:
byte[] image = File.ReadAllBytes(path)
Then you can insert it in your database.
tip:
When you use a SqlConnection, surround it with the using keyword:
using (var connection = new SqlConnection(connectionString))
{
}
来源:https://stackoverflow.com/questions/6333553/problem-with-storing-images-in-sql-server