Simple C # connection to .accdb file

All I want to do is get the data from the tables in the .accdb file.

Here is my complete application:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Microland.accdb;Persist Security Info=False;");
            myConn.Open();
            OleDbCommand myQuery = new OleDbCommand("select CustID from Customers WHERE CustID = 1;", myConn);
            OleDbDataReader myReader = myQuery.ExecuteReader();
             if(myReader.HasRows)
             {
                myReader.Read();
                label1.Text = myReader.ToString();
             }
            myConn.Close();

        }
    }
}

I think that I am missing some of them at the very top, or my code is somewhat broken because I press button1, the text of label1 changes to System.Data.OleDb.OleDbDataReader.

This is also the correct way to connect to the access database (.accdb). Do I need to do this walkthrough so that it or it has nothing to do with what I need to do?

Thanks for any info! Very much appreciated

+4
source share
2 answers

myReader.ToString(), , . , "System.Data.OleDb.OleDbDataReader" .

, , . , . .

Get*().

+3
label1.Text = myReader["CustID"] as string; // if nullable field

label1.Text = (string)myReader["CustID"] // if not null
0

Source: https://habr.com/ru/post/1615337/


All Articles