Problem with storing images in sql server

早过忘川 提交于 2021-01-28 07:08:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!