我是 C# 编程的新手。我在使用时遇到问题FileStream
。我想通过在数据库中搜索人的 ID 从数据库中检索图片。它的工作原理!但是当我尝试两次检索此人的图片时(两次插入相同的 ID)。它会给 IOException
"The process cannot access the file 'C:\Users\dor\Documents\Visual Studio 2010\Projects\studbase\studbase\bin\Debug\foto.jpg' because it is being used by another process."
我有 1 个按钮 --> buttonLoad
| 1个图片框-->pictureBoxPictADUStudent
这是 buttonLoad 上的代码
string sql = "SELECT Size,File,Name FROM studgeninfo WHERE NIM = '"+textBoxNIM.Text+"'";
MySqlConnection conn = new MySqlConnection(connectionSQL);
MySqlCommand comm = new MySqlCommand(sql, conn);
if (textBoxNIM.Text != "")
{
conn.Open();
MySqlDataReader data = comm.ExecuteReader();
while (data.Read())
{
int fileSize = data.GetInt32(data.GetOrdinal("size"));
string name = data.GetString(data.GetOrdinal("name"));
using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
{
byte[] rawData = new byte[fileSize];
data.GetBytes(data.GetOrdinal("file"), 0, rawData, 0, fileSize);
fs.Write(rawData, 0, fileSize);
fs.Dispose();
pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);
}
}
conn.Close();
}
else
{
MessageBox.Show("Please Input Student NIM ", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}