<small id='ZckbE'></small><noframes id='ZckbE'>

      <i id='ZckbE'><tr id='ZckbE'><dt id='ZckbE'><q id='ZckbE'><span id='ZckbE'><b id='ZckbE'><form id='ZckbE'><ins id='ZckbE'></ins><ul id='ZckbE'></ul><sub id='ZckbE'></sub></form><legend id='ZckbE'></legend><bdo id='ZckbE'><pre id='ZckbE'><center id='ZckbE'></center></pre></bdo></b><th id='ZckbE'></th></span></q></dt></tr></i><div id='ZckbE'><tfoot id='ZckbE'></tfoot><dl id='ZckbE'><fieldset id='ZckbE'></fieldset></dl></div>
        • <bdo id='ZckbE'></bdo><ul id='ZckbE'></ul>
        <tfoot id='ZckbE'></tfoot>

        <legend id='ZckbE'><style id='ZckbE'><dir id='ZckbE'><q id='ZckbE'></q></dir></style></legend>

      1. C# 将图片插入 Ms Access

        时间:2023-06-05
        <i id='9DnNr'><tr id='9DnNr'><dt id='9DnNr'><q id='9DnNr'><span id='9DnNr'><b id='9DnNr'><form id='9DnNr'><ins id='9DnNr'></ins><ul id='9DnNr'></ul><sub id='9DnNr'></sub></form><legend id='9DnNr'></legend><bdo id='9DnNr'><pre id='9DnNr'><center id='9DnNr'></center></pre></bdo></b><th id='9DnNr'></th></span></q></dt></tr></i><div id='9DnNr'><tfoot id='9DnNr'></tfoot><dl id='9DnNr'><fieldset id='9DnNr'></fieldset></dl></div>

              <tbody id='9DnNr'></tbody>
                  <bdo id='9DnNr'></bdo><ul id='9DnNr'></ul>

                  <legend id='9DnNr'><style id='9DnNr'><dir id='9DnNr'><q id='9DnNr'></q></dir></style></legend>
                • <small id='9DnNr'></small><noframes id='9DnNr'>

                  <tfoot id='9DnNr'></tfoot>
                • 本文介绍了C# 将图片插入 Ms Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我要感谢所有在上一个问题中提供帮助的人.但现在,我对另一种说法有问题,即 saveimageto MS 访问.首先我想问一下,在ms access数据库上,Datatype应该放附件吗?

                  I would like to thanks to all who helped in last question. but now, i have problem with another statement which is saveimageto MS access. First, I would like to ask, on ms access database, Datatype should put attachment?

                  我的代码:

                  private void button2_Click(object sender, EventArgs e)
                          {
                  
                              OleDbCommand cmd = new OleDbCommand();
                              cmd.CommandType = CommandType.Text;
                              cmd.CommandText = "insert into Table1 (id,picture) values ('" + textBox1.Text +  "')";
                  
                              cmd.Connection = con;
                              con.Open();
                              cmd.ExecuteNonQuery();
                              System.Windows.Forms.MessageBox.Show("Created", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                              con.Close();
                  
                          }
                  

                  我使用 openFIledialog 将我的图片插入图片框.

                  I insert my picture to picturebox using openFIledialog.

                  推荐答案

                  首先,使用参数.永远不要连接 SQL 命令的字符串,因为它会向 SQL 注入开放.这是一个易于遵循的良好做法,可以避免您将来遇到很多麻烦.

                  First, use parameters. NEVER concat strings for SQL commands, as it opens itself to SQL Injection. That's an easy to follow good practice that'll avoid you a lot of trouble in the future.

                  也就是说,这样的事情应该可以工作:

                  That said, something like this should work:

                  // You've got the filename from the result of your OpenDialog operation
                  var pic = File.ReadAllBytes(yourFileName);
                  OleDbCommand cmd = new OleDbCommand();
                  cmd.CommandType = CommandType.Text;
                  cmd.CommandText = "insert into Table1 (id, picture) values (@p1, @p2)";
                  cmd.Parameters.AddWithValue("@p1", TextBox1.Text);
                  cmd.Parameters.AddWithValue("@p2", pic);
                  cmd.ExecuteNonQuery();
                  

                  在这里从记忆中引用,但如果该代码给您带来麻烦,请告诉我.如果我没记错的话,应该可以这样.

                  Citing from memory here, but let me know if that code gives you trouble. If I remember correctly, something like that should work.

                  EDIT.- 如果您在 PictureBox 控件上预加载图像,将该图像转换为字节数组,然后将该字节数组用作第二个参数.

                  EDIT.- If you're preloading the image on a PictureBox control, convert that image to a byte array and then use that byte array as the second parameter.

                  编辑 (2).- 稍微澄清一下.如果您要从文件中获取图像,那么您就有了它的路径;那么你可以使用 File.ReadAllBytes(string path).在我的示例中,我假设 yourFileName 是成功 OpenDialog 操作后所选文件的文件和路径名.所以你可以这样使用它:

                  EDIT (2).- A little clarification. If you're are getting your image from a file, you have a path to it; then you can use File.ReadAllBytes(string path). In my example I was assuming that yourFileName was the file and path name of the selected file after a successful OpenDialog operation. So you can use it like this:

                  byte[] fromPath = File.ReadAllBytes(@"C:wallsaurora.jpg");
                  

                  然后您将图像存储到字节数组 fromPath 中,转换为字节并准备好在插入命令中使用,如上所示.

                  and you'd store into the byte array fromPath the image, converted to bytes and ready to be used in an insertion command as seen above.

                  但是如果您从图片框控件获取图像,情况会有些不同:

                  BUT if you're getting your image from a picture box control, things are a little different:

                  MemoryStream ms = new MemoryStream();
                  pictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg);
                  byte[] fromControl = ms.GetBuffer();
                  

                  在该示例中,我创建了一个 MemoryStream,用图片框控件的内容填充它,然后将其传递给字节数组,我们再次准备将其用作参数用于我们的插入查询.

                  In that example I've created a MemoryStream, filled it with the content of the picturebox control and then passed it to the byte array, and again we're ready to use it as a parameter for our insertion query.

                  哦,别忘了加

                  using System.IO;
                  using System.Drawing;
                  

                  到你的使用.

                  这篇关于C# 将图片插入 Ms Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:c# 如果记录存在则更新,否则插入新记录 下一篇:在 asp.net 中插入后获取主键(visual basic)

                  相关文章

                  最新文章

                • <small id='SqxMo'></small><noframes id='SqxMo'>

                    <tfoot id='SqxMo'></tfoot>
                  1. <i id='SqxMo'><tr id='SqxMo'><dt id='SqxMo'><q id='SqxMo'><span id='SqxMo'><b id='SqxMo'><form id='SqxMo'><ins id='SqxMo'></ins><ul id='SqxMo'></ul><sub id='SqxMo'></sub></form><legend id='SqxMo'></legend><bdo id='SqxMo'><pre id='SqxMo'><center id='SqxMo'></center></pre></bdo></b><th id='SqxMo'></th></span></q></dt></tr></i><div id='SqxMo'><tfoot id='SqxMo'></tfoot><dl id='SqxMo'><fieldset id='SqxMo'></fieldset></dl></div>
                      <bdo id='SqxMo'></bdo><ul id='SqxMo'></ul>

                    1. <legend id='SqxMo'><style id='SqxMo'><dir id='SqxMo'><q id='SqxMo'></q></dir></style></legend>