简体   繁体   English

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

[英]How do I store a image BLOB in an access database after getting the name from openfiledialog?

I am developing a C# application that has an Access database. 我正在开发具有Access数据库的C#应用​​程序。 What I want to do is allow a user to select an image through an "openfiledialog." 我要做的是允许用户通过“ openfiledialog”选择图像。 I then want to store the image in one of the table of the access database in a BLOB field. 然后,我想将图像存储在BLOB字段中的访问数据库的表之一中。 I have searched over the internet, but found nothing helpful. 我已经在互联网上进行搜索,但没有发现任何帮助。 I hope you can help me. 我希望你能帮助我。

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");

     }

}

but now how can I be sure that is stored in the access database? 但是现在如何确定存储在访问数据库中?

Example: 例:

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();
}

What you will want to do is something similar to the following. 您将要做的事情类似于以下内容。

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)并将byte []保存在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从BLOB转换图像后如何调整图像大小? - How do I resize an image after converting it from a BLOB? 如何从Azure Blob存储访问图像 - How to access an image from azure blob storage 如何将图像保存到 Azure Blob 并使用特定字段命名并返回 Blob Url 并将其保存到 .net core 2.2 中的数据库 - How can i save an image to a Azure Blob and name it with specific fields and return the Blob Url and save it to database in .net core 2.2 从OpenFileDialog获取文件大小? - Getting filesize from OpenFileDialog? 从MySQL数据库将图像另存为BLOB时参数无效 - Parameter not valid when getting Image saved as BLOB from MySQL Database 如何使用 OpenFileDialog 到 select 一个文件夹? - How do I use OpenFileDialog to select a folder? 如何将 blob 图像从数据库转换为 html 图像 src - How can I convert blob image from database to html image src 如何使用 Streamwrite 访问 OpenFileDialog? - how to access OpenFileDialog with Streamwrite? 如何显示来自mysql(blob)中数据库的图像到网页中的“图像”控件 - How can i show an image from a database in mysql(blob), to an “Image” control in a web page 如何将图像的BLOB保存到sql数据库而不先将其保存到文件系统? - How do I save the BLOB of an image into a sql database without saving it to the filesystem first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM