【问题标题】:Unable to display image on DataGridView from Microsoft Access无法从 Microsoft Access 在 DataGridView 上显示图像
【发布时间】:2020-01-06 17:48:56
【问题描述】:

我将 MS Access 数据库中的数据存储在 DataTable 中,然后在 DataGridView 上显示。

所有列都显示,除了图像列。我已将图像作为“OLE 对象”存储在数据库中。我想在最后一列显示图像,但当表单加载时出现错误:

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

【问题讨论】:

  • 你确定jtable吗?
  • @JQSOFT 对不起,我的意思是 Datagridview
  • 您现在可能想阅读thisthis
  • 我搞定了。我必须在 OLE OBJECT COLUMN 的 msaccess 数据库中将图像保存为 byte[] 数组。然后在检索结果时将结果转换为 byte[]。 @JQSOFT 谢谢。

标签: c# database image ms-access datagridview


【解决方案1】:
//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();
        }


    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多