无法在DataGridView中从Microsoft Access显示图像。

huangapple go评论90阅读模式
英文:

Unable to display image on DataGridView from Microsoft Access

问题

I am storing data from MS Access database in DataTable and then displaying it on DataGridView.

All the columns are showing except the image column. I have stored the image in the database as "OLE Object". I want to display the image in the last column, though when the form loads I get an error:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ShowDataInDatagridviewFromAccessDatabase
{
    public partial class Form1 : Form
    {
        OleDbConnection accessDatabaseConnection = null;
        string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Authentic\Documents\Database1.accdb";
        string sqlQuery = "SELECT * FROM Table1";

        public Form1()
        {
            accessDatabaseConnection = new OleDbConnection(connectionString);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                accessDatabaseConnection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, accessDatabaseConnection);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                dataGridView1.DataSource = dataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (accessDatabaseConnection != null)
                {
                    accessDatabaseConnection.Close();
                }
            }
        }
    }
}

无法在DataGridView中从Microsoft Access显示图像。

英文:

I am storing data from MS Access database in DataTable and then displaying it on DataGridView.

All the columns are showing except the image column. I have stored the image in the database as "OLE Object". I want to display image in the last column, though when the form loads I get error:

无法在DataGridView中从Microsoft Access显示图像。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ShowDataInDatagridviewFromAccessDatabase
{
    public partial class Form1 : Form
    {
        OleDbConnection acceddDatabaseConnection = null;
        string connectionSttring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Authentic\Documents\Database1.accdb";
        string sqlQuery = "SELECT * FROM Table1";

        public Form1()
        {
            acceddDatabaseConnection = new OleDbConnection(connectionSttring);
            InitializeComponent();    
        }

        private void Form1_Load(object sender, EventArgs e)
        {    
            try
            {
                acceddDatabaseConnection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, acceddDatabaseConnection);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                dataGridView1.DataSource = dataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (acceddDatabaseConnection != null)
                {
                    acceddDatabaseConnection.Close();
                }
            }
        }
    }
}

答案1

得分: 1

以下是代码的翻译部分:

// 从MS Access数据库检索数据并显示在DataGridView上的函数
public void populateDataGridView()
{
    // 首先,在从MS Access数据库中填充数据之前,清除所有行。在清除之前,请检查DataGridView行是否为空。
    if (dataGridView1.Rows.Count > 0)
    {
        dataGridView1.Rows.Clear();
    }

    try
    {
        accessDatabaseConnection.Open();
        // OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, accessDatabaseConnection);
        OleDbCommand command = new OleDbCommand(selectDataFromMSAccessDatabaseQuery, accessDatabaseConnection);
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[8].GetType());
            dataGridView1.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), (byte[])reader[8]);
        }

        reader.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.StackTrace);
    }
    finally
    {
        // 最后关闭MS Access数据库连接
        if (accessDatabaseConnection != null)
        {
            accessDatabaseConnection.Close();
        }
    }
}

来源:
http://mauricemuteti.info/how-to-connect-to-access-database-and-display-data-and-images-in-datagridview-in-c-sharp-windows-application/

英文:
//Function for retrieving data from ms access database and displaying it on DataGridView
public void populateDataGridView()
{
    //First, clear all rows before populating datagridview with data from MS Access Database. Check if datagridview rows are empty before clearing.
    if (dataGridView1.Rows.Count > 0)
    {
        dataGridView1.Rows.Clear();
    }

    try
    {
        accessDatabaseConnection.Open();
        //OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, acceddDatabaseConnection);
        OleDbCommand command = new OleDbCommand(selectDataFromMSAccessDatabaseQuery, accessDatabaseConnection);
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[8].GetType());
            dataGridView1.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), (byte[])reader[8]);
        }

        reader.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.StackTrace);
    }
    finally
    {
        //Finally Close MS Access Database Connection
        if (accessDatabaseConnection != null)
        {
            accessDatabaseConnection.Close();
        }


    }
}

Source :
http://mauricemuteti.info/how-to-connect-to-access-database-and-display-data-and-images-in-datagridview-in-c-sharp-windows-application/

huangapple
  • 本文由 发表于 2020年1月7日 01:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定