OleDbConnectionStringBuilder

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

Microsoft Excel:
OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
builder.Provider = "Microsoft.ACE.OLEDB.12.0"; // "Microsoft.Jet.OLEDB.4.0"
builder.DataSource = @"|DataDirectory|/ExcelName.xls"; // "|DataDirectory|" <=> "."
builder["Extended Properties"] = "Excel 8.0;HDR=No;IMEX=1"; // "HDR=No" 包含首行;"IMEX=1" <=> "'"。
OleDbConnection oledb = new OleDbConnection(builder.ConnectionString);
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", oledb))
{
    DataTable table = new DataTable();
    table.BeginLoadData();
    adapter.Fill(table);
    table.EndLoadData();
}

Microsoft Access:
OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
builder.Provider = "Microsoft.ACE.OLEDB.12.0"; // "Microsoft.Jet.OLEDB.4.0"
builder.DataSource = @"|DataDirectory|/AccessName.mdb"; // "*.accdb"
builder["Jet OLEDB:Database Password"] = "jinzhexian";
OleDbConnection oledb = new OleDbConnection(builder.ConnectionString);
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [TableName]", oledb))
{
    DataTable table = new DataTable();
    table.BeginLoadData();
    adapter.Fill(table);
    table.EndLoadData();
}

你可能感兴趣的:(ADO.NET)