通过using语句实现非GC资源的自动回收。
还需要有try…catch…之类的异常检测语句,检测file.exist。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;using System.Data.OleDb;
using System.Data.SqlClient;namespace ReadAccess
{ class Program { static void Main(string[] args) { using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=school.mdb")) { conn.Open();string sql = "select * from sheet1 where student='stu2'";
OleDbCommand command;
command = new OleDbCommand(sql, conn);
using (OleDbDataReader reader = command.ExecuteReader())
{ //1次读取1个记录 while (reader.Read()) {for (int i = 0; i < reader.FieldCount; i++)
{Console.Write("{0} ", reader[i]);
}
Console.WriteLine();
}
} } } } }