英文:
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();
}
}
}
}
}
英文:
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:
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();
}
}
}
英文:
//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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论