private void button3_Click(object sender, EventArgs e)
        {
            byte[] imagebytes = null;
            OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mndb.mdb");
            MyConnection.Open();
            OleDbCommand cmd1 = new OleDbCommand("select Sphoto from Student where Sno='" + textBox1.Text.Trim() + "'", MyConnection);
            OleDbDataReader reader = cmd1.ExecuteReader();
            while (reader.Read())
            {
                imagebytes = (byte[])reader.GetValue(1);
            }
            reader.Close();
            cmd1.Clone();
            //MyConnection.Close();
            MemoryStream ms = new MemoryStream(imagebytes);
            Bitmap bmpt = new Bitmap(ms);
            pictureBox1.Image = bmpt;
            byte[] imagebytes = null这行运行出现错误:System.IndexOutOfRangeException: 索引超出了数组界限  我就是想从数据库里读出照片,数据库用ACCESS,里面插入的图片为长二进制数据
    

解决方案 »

  1.   

    byte[] imagebytes = null;这句不应该会出错阿
      

  2.   

    GetValue(0);
      

  3.   

    改了后报错误为Bitmap bmpt = new Bitmap(ms);参数无效....
      

  4.   

    肯定是GetValue(1)怎么能改成(0)呢
      

  5.   

    System.IndexOutOfRangeException: 索引超出了数组界限。
       在 System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal)
       在 System.Data.OleDb.OleDbDataReader.GetValue(Int32 ordinal)
       在 WindowsFormsApplication8.Update.button3_Click(Object sender, EventArgs e) 位置 D:\WindowsFormsApplication8\WindowsFormsApplication8\Update.cs:行号 38
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)以上是错误信息,38行是 imagebytes = (byte[])reader.GetValue(1);
      

  6.   

    刚开始看错行了,是 imagebytes  (byte[])reader.GetValue(1);这行
      

  7.   

    那就是你 imagebytes = (byte[])reader.GetValue(1);这里有问题嘛,怎么你开始说 byte[] imagebytes = null出错。。
      

  8.   


            private void button3_Click(object sender, EventArgs e)
            {
                byte[] imagebytes = null;
                OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mndb.mdb");
                MyConnection.Open();
                OleDbCommand cmd1 = new OleDbCommand("select Sphoto from Student where Sno='" + textBox1.Text.Trim() + "'", MyConnection);
                OleDbDataReader reader = cmd1.ExecuteReader();
                while (reader.Read())
                {
                    imagebytes = (byte[])reader.GetValue(1);
                }
                reader.Close();
                cmd1.Clone();
                //MyConnection.Close();
                MemoryStream ms = new MemoryStream(imagebytes);
                Bitmap bmpt = new Bitmap(ms);
                pictureBox1.Image = bmpt;
                OleDbDataAdapter adp = new OleDbDataAdapter("select * from Student where Sno='" + textBox1.Text.Trim() + "'", MyConnection);
                DataSet ds = new DataSet();
                adp.Fill(ds, "Student");
                OleDbCommand cmd = new OleDbCommand("select * from Student where Sno='" + textBox1.Text.Trim() + "'", MyConnection);
                if (null != cmd.ExecuteScalar())
                {
                    this.Sno.Text = ds.Tables[0].Rows[0]["sno"].ToString();
                    this.Sname.Text = ds.Tables[0].Rows[0]["sname"].ToString();
                    this.Ssex.Text = ds.Tables[0].Rows[0]["ssex"].ToString();
                    this.Sage.Text = ds.Tables[0].Rows[0]["sage"].ToString();
                    this.Sethiopia.Text = ds.Tables[0].Rows[0]["sethiopia"].ToString();
                    this.Shome.Text = ds.Tables[0].Rows[0]["shome"].ToString();
                    this.Smobile.Text = ds.Tables[0].Rows[0]["smobile"].ToString();
                    this.Sdept.Text = ds.Tables[0].Rows[0]["sdept"].ToString();
                    //this.pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])ds.Tables[0].Rows[0]["sphoto"].ToString()));
                    MyConnection.Close();
                }
                else
                {
                    MessageBox.Show("无此学号学生", "提示");
                }
                        }
    整个事件的代码,开始没加读出照片那些,运行没问题的,加了照片度不出来~~
      

  9.   

    Sphoto在Student表里,格式是OLE对象,插入图片  
          private Byte[] getphoto(string photopath)
            {
                string str = photopath;
                FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
                Byte[] bytBLOBData = new Byte[file.Length];
                file.Read(bytBLOBData, 0, bytBLOBData.Length);
                file.Close();
                return bytBLOBData;
            }        private void pictureBox1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "jpg|*jpg|bmp|*.bmp|gif|*.gif|jpeg|*.jpeg|ico|*.ico";
                openFileDialog1.ShowDialog();            if (openFileDialog1.FileName != "")
                    this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }插入图片后数据里为长二进制数据
      

  10.   

    你先到查询分析器里面把你那个sql语句执行一次看看,有没有数据
      

  11.   

    有数据,是“<二进制数据>”
      

  12.   

    索引出界不是只有数组才有索引
    reader.GetValue(1); 这个方法就没有 索引出界的问题??
    .NET Framework 类库
    OleDbDataReader.GetValue 方法获取以本机格式表示的指定序号处的列的值。命名空间:System.Data.OleDb
    程序集:System.Data(在 system.data.dll 中)
     语法
    Visual Basic(声明)Public Overrides Function GetValue ( _
        ordinal As Integer _
    ) As ObjectVisual Basic(用法)Dim instance As OleDbDataReader
    Dim ordinal As Integer
    Dim returnValue As ObjectreturnValue = instance.GetValue(ordinal)C#public override Object GetValue (
        int ordinal
    )C++public:
    virtual Object^ GetValue (
        int ordinal
    ) overrideJ#public Object GetValue (
        int ordinal
    )JScriptpublic override function GetValue (
        ordinal : int
    ) : Object参数ordinal    从零开始的列序号。 返回值
    要返回的值。
      

  13.   

    imagebytes = (byte[])reader.GetValue(1);2imagebytes = (byte[])reader.GetValue(0);
    只有一列怎么可能取到第二列去了
      

  14.   


    看看 imagebytes 有没有数据