如何将MicrosoftAccess数据库表放入数据集中 [英] how to get the MicrosoftAccess database table into dataset

查看:91
本文介绍了如何将MicrosoftAccess数据库表放入数据集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/diva/Desktop/stores.mdb");
DataSet ds = new DataSet();
System.Data.OleDb.OleDbDataAdapter da;

 con.Open();
da = new System.Data.OleDb.OleDbDataAdapter(sql1 = "SELECT * FROM  dbTableName", con);
da.Fill(ds, dbTableName);
con.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "dbTableName";



我无法从数据库中获取表,请帮助我..



I cant able to get table from the database please help me..

推荐答案

您好,请尝试以下操作:
Hi, try this:
string connString = 
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database.mdb";

DataTable results = new DataTable();

using(OleDbConnection conn = new OleDbConnection(connString))
{
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM TableName", conn);

    conn.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

    adapter.Fill(results);
}

请根据您的需要更改数据库路径和表名.

Please change the DB path and table name as per yours.


using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (OleDbConnection oCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\dinesht\Desktop\MyDB.mdb"))
            {
                oCon.Open();

                OleDbDataAdapter oAdpt = new OleDbDataAdapter("SELECT * FROM tbl", oCon);

                DataSet __ds = new DataSet();

                oAdpt.Fill(__ds, "tbl");

                // THE DATASET IS FILLED NOW. YOU CAN USE THE DATA //

                oCon.Close();
            }
        }
    }
}


这篇关于如何将MicrosoftAccess数据库表放入数据集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆