且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将MicrosoftAccess数据库表放入数据集中

更新时间:2023-10-10 19:21:28

您好,请尝试以下操作:
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();
            }
        }
    }
}