从openfiledialog获取名称后,如何将图像BLOB存储在访问数据库中?

我正在开发一个具有Access数据库的C#应用​​程序。我想要做的是允许用户通过“openfiledialog”选择图像。然后,我想将图像存储在BLOB字段中的访问数据库的一个表中。我通过互联网搜索,但没有发现任何帮助。我希望你能帮助我。
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{

     // textBox1.Show(openFileDialog1.FileName.ToString());
     // MessageBox.Show(openFileDialog1.FileName.ToString());
     textBox1.Text = openFileDialog1.FileName.ToString();

     String filename = openFileDialog1.FileName.ToString();
     byte[] buffer = File.ReadAllBytes(filename);

     using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Policias.accdb"))

     using (var cmd = conn.CreateCommand())
     {                 
        cmd.CommandText = "INSERT INTO DetallesMunicipio(imagen) VALUES (@imagen)";
        cmd.Parameters.AddWithValue("@imagen", buffer);
        conn.Open();
        cmd.ExecuteNonQuery();

     }
     }
     else
     {
         MessageBox.Show("Porfavor selecciona una imagen");

     }

}
但是现在我如何确定存储在访问数据库中?     
已邀请:
例:
string filename = "foo.txt"; // TODO: fetch from file dialog
byte[] buffer = File.ReadAllBytes(filename);

using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=foo.mdb"))
using (var cmd = conn.CreateCommand())
{
    cmd.CommandText = "INSERT INTO MyTable VALUES (@Name, @Data)";
    cmd.Parameters.AddWithValue("@Name", filename);
    cmd.Parameters.AddWithValue("@Data", buffer);
    cmd.ExecuteNonQuery();
}
    
您想要做的是类似于以下内容。
using (OpenFileDialog fileDialog = new OpenFileDialog){
   if(fileDialog.ShowDialog == DialogResult.OK){
       using (System.IO.FileInfo fileToSave = new System.IO.FileInfo(fileDialog.FilePath)){
          MemoryStream ms = System.IO.FileStream(fileToSave.FullNae, IO.FileMode.Open);
           //Here you can copy the ms over to a byte array to save into your blob in your database.
       }

   }
}
    
我会使用File.ReeadAllBytes(file)并将字节[]保存在DB中。     

要回复问题请先登录注册