如何在Access 2007中存储jpeg [英] How to store jpeg in Access 2007

查看:76
本文介绍了如何在Access 2007中存储jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我从这个网站获得了很多帮助

在这里,我面临一个简单的问题,但无法指出,因此,我需要大家的紧急帮助.
我正在使用Windows 7,VS C#2008,Access 2007
我的Access数据库具有OLE对象的1个照片列.
这是C#的代码,如果它没有照片部分就可以正常工作-在代码抛出异常后,请提及它突出显示所有重要部分:叹息:

Hi All

I got lots of help from this site

Here I am facing 1 simple problem but can''t point it out, So I need urgent help from you all.
I am using Windows 7, VS C# 2008, Access 2007
My Access database have 1 photo column of OLE Object.
Here is the code of C# which is working fine if it work without photo part - After the code Exception which is thrown is mention pls see it All the important section are highlighted:sigh:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Globalization;
using System.IO;
using Microsoft.VisualBasic;
namespace StudentInformation
{
    public partial class Form4 : Form
    {
        OleDbCommand aCommand;
        OleDbConnection aConnection;
        MemoryStream ms;
        //byte[] photo_aray;
        String queryStr;
        //All Variables are decleared here
        //photo
                
        public Form4()
        {
            InitializeComponent();
            aConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\KVSoftware\\KVStudent\\KV1StudentInformation.mdb");
            Console.WriteLine("Form4 started");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                 */entered values are stored in local variables for eg. 
                v_admission_no=textBox3.Text;
                v_student_name=textBox6.Text;
                v_admitted_class=textBox11.Text;
                v_admission_date = dateTimePicker1.Value.ToString(); */
                
                queryStr="INSERT INTO PersonalDetail(admission_no,student_name,admitted_class,admission_date,present_class,present_section,roll_no,dob,gender,caste,minority_status,house,cbse_xth_roll,previous_school,conveyance_mode,remark,photo) VALUES(''" + v_admission_no + "'',''" + v_student_name + "'',''" + v_admitted_class +
                    "'',''" + v_admission_date + "'',''" + v_present_class + "'',''" + v_present_section + "'',''" + v_roll_no + "'',''" + v_dob +
                    "'',''" + v_gender + "'',''" + v_caste + "'',''" +  v_minority_status + "'',''" + v_house + "'',''" + v_cbse_xth_roll +
                    "'',''" + v_previous_school + "'',''" + v_conveyance_mode + "'',''" + v_remark + "'',@photo)";     // 
                Console.WriteLine("query Build");
                conv_photo();
                aConnection.Open();
                aCommand = new OleDbCommand(queryStr, aConnection);
                Console.WriteLine("query Executed on Connection");
                int check=aCommand.ExecuteNonQuery();
                Console.WriteLine("query Executed");
                if (check == 1)
                {
                    //pictureBox1.Visible = true;
                    label1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
            finally
            {
                MessageBox.Show("Finally of Personal");
                aConnection.Close();
            }
         }
        void conv_photo()
        {
            try
            {
                //converting photo to binary data
                if (pictureBox1.Image != null)
                {
                    //using FileStream:(will not work while updating, if image is not changed)
                    //FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                    //byte[] photo_aray = new byte[fs.Length];
                    //fs.Read(photo_aray, 0, photo_aray.Length);  
                    //using MemoryStream:
                    Console.WriteLine("Cov1");
                    ms = new MemoryStream();
                    Console.WriteLine("Cov2");
                    pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                    Console.WriteLine("Cov3");
                    Console.WriteLine(ms.Length);
                    byte[] photo_aray = new byte[ms.Length];
                    Console.WriteLine("Cov4");
                    ms.Position = 0;
                    Console.WriteLine("Cov5");
                    ms.Read(photo_aray, 0, photo_aray.Length);
                    Console.WriteLine("Cov6");
                    aCommand.Parameters.AddWithValue("@photo", photo_aray);
                    Console.WriteLine("Cov7");
                }            }
            catch (Exception ne)
            {
                Console.WriteLine("Error: {0}", ne.Message);
            }
        }
     
        private void tabPage2_Enter(object sender, EventArgs e)
        {
            label52.Text = v_admission_no; label53.Text = v_student_name;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
            DialogResult res = openFileDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            } 
        }
   }
}


例外部分
这是从登录表返回的数据
Form4开始
查询Build
Cov1
Cov2
类型``System.NullReferenceException''的第一次机会异常发生在StudentInformation.exe中类型``System.Data.OleDb.OleDbException''的第一次机会异常发生在System.Data.dll
Cov3
128755
Cov4
Cov5
Cov6
错误:对象引用未设置为对象的实例.查询在连接上执行
错误:没有为一个或多个必需参数提供任何值.线程0x250已退出,代码为0(0x0).
线程0x1598已退​​出,代码为0(0x0).
程序"[1480] StudentInformation.vshost.exe:已托管"已退出,代码为0(0x0).


Exception Part
This is the returned data from Login table
Form4 started
query Build
Cov1
Cov2
A first chance exception of type ''System.NullReferenceException'' occurred in StudentInformation.exeA first chance exception of type ''System.Data.OleDb.OleDbException'' occurred in System.Data.dll
Cov3
128755
Cov4
Cov5
Cov6
Error: Object reference not set to an instance of an object.query Executed on Connection
Error: No value given for one or more required parameters.The thread 0x250 has exited with code 0 (0x0).
The thread 0x1598 has exited with code 0 (0x0).
The program ''[1480] StudentInformation.vshost.exe: Managed'' has exited with code 0 (0x0).

推荐答案

我的输出存在问题由您的程序生成.如果在将"Cov2"字符串写入控制台后引发了异常,它将被conv_photo方法中的try catch捕获,此后,控制流将在try catch块之后,从而有效地从方法conv_photo返回. > 在您的输出中(我猜可能是模拟的),该方法似乎继续执行,而根据上述论点是不可能的.

修改:
OLE DB .NET提供程序不支持命名参数.当CommandType设置为Text时,必须使用问号(?)占位符.

例如:
I have an issue with the output supposedly generated by your program. If an exception is thrown after "Cov2" string has been written to console it would be caught by the try catch in method conv_photo and after that the flow of control would be after the try catch block thus effectively returning from method conv_photo.
In your (I would guess simulated) output the method seems to continue with its execution which is impossible as per above argumentation.

Modification:
The OLE DB .NET Provider does not support named parameters. When CommandType is set to Text the question mark (?) placeholder must be used.

For example:
queryStr = "INSERT INTO PersonalDetail(admission_no,student_name,admitted_class,admission_date,present_class,present_section,roll_no,dob,gender,caste,minority_status,house,cbse_xth_roll,previous_school,conveyance_mode,remark,photo) VALUES('" + v_admission_no + "','" + v_student_name + "','" + v_admitted_class +                    "','" + v_admission_date + "','" + v_present_class + "','" + v_present_section + "','" + v_roll_no + "','" + v_dob +                    "','" + v_gender + "','" + v_caste + "','" +  v_minority_status + "','" + v_house + "','" + v_cbse_xth_roll +                    "','" + v_previous_school + "','" + v_conveyance_mode + "','" + v_remark + "', ? )"; //changed @photo to ?



在conv_photo中



In conv_photo

// Adding the parameters will have to be in the same order as they appear in the query
command.Parameters.Add(photo_array, SqlDbType.Binary);


结束修改

最好的问候,

曼弗雷德(Manfred)


End modification

Best Regards,

Manfred


这篇关于如何在Access 2007中存储jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆