主题:using的用法
我在写一个读取ACCESS数据库的类,在网上抄到一段代码,其中用到using,但我对这个using所知甚少,请哪位详细介绍一下using的用法吧
代码如下
类文件:DataConnection.cs
其中的全部代码
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
namespace CostCal
{
public class DataConnection
{
public object Get_AllRecords()
{
DataConnection dc = new DataConnection();
OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
connectStringBuilder.DataSource = @"Record.mdb";
connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
{
DataSet ds = new DataSet();
OleDbCommand cmdstr = new OleDbCommand("SELECT RecordDate, RecordType, RecordCode, RecordDetails from RecordData", cn);
cn.Open();
using (OleDbDataReader dr = cmdstr.ExecuteReader())
{
ds.Load(dr, LoadOption.OverwriteChanges, new string[] { "RecordData" });
return ds.Tables["RecordData"].DefaultView;
}
}
}
}
}
代码如下
类文件:DataConnection.cs
其中的全部代码
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
namespace CostCal
{
public class DataConnection
{
public object Get_AllRecords()
{
DataConnection dc = new DataConnection();
OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
connectStringBuilder.DataSource = @"Record.mdb";
connectStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
using (OleDbConnection cn = new OleDbConnection(connectStringBuilder.ConnectionString))
{
DataSet ds = new DataSet();
OleDbCommand cmdstr = new OleDbCommand("SELECT RecordDate, RecordType, RecordCode, RecordDetails from RecordData", cn);
cn.Open();
using (OleDbDataReader dr = cmdstr.ExecuteReader())
{
ds.Load(dr, LoadOption.OverwriteChanges, new string[] { "RecordData" });
return ds.Tables["RecordData"].DefaultView;
}
}
}
}
}