我在输入文件中显示图像然后我想将此图像保存到数据库。我知道如何将 varbinary 或图像存储到数据库但我不知道如何访问输入文件?
SqlConnection con = new SqlConnection(stcon);
SqlCommand command = new SqlCommand();
string path = "";
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
MemoryStream tmpStream = new MemoryStream();
img.Save(tmpStream, ImageFormat.Png); // change to other format
tmpStream.Seek(0, SeekOrigin.Begin);
byte[] imgBytes = new byte[100000];
tmpStream.Read(imgBytes, 0, 100000);
command.CommandText = "INSERT INTO image(image) VALUES (:image)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "image";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery();