以下代碼闡明了如何使用 SqlDataAdapter 對象發出可生成 DataSet 或 DataTable 的命令
using System
using System
public DataTable RetrieveRowsWithDataTable()
{
using ( SqlConnection conn = new SqlConnection(connectionString) )
{
conn
SqlCommand cmd = new SqlCommand(
cmd
SqlDataAdapter adapter = new SqlDataAdapter( cmd );
DataTable dataTable = new DataTable(
adapter
return dataTable;
}
}
使用 SqlAdapter 生成 DataSet 或 DataTable
如何使用 SqlDataReader 來檢索多個行以下代碼片段闡明了可檢索多個行的 SqlDataReader 方法
using System
using System
using System
public SqlDataReader RetrieveRowsWithDataReader()
{
SqlConnection conn = new SqlConnection(
SqlCommand cmd = new SqlCommand(
cmd
try
{
conn
// Generate the reader
// the connection to be closed when the reader object is closed
return( cmd
}
catch
{
conn
throw;
}
}
// Display the product list using the console
private void DisplayProducts()
{
SqlDataReader reader = RetrieveRowsWithDataReader();
try
{
while (reader
{
Console
reader
reader
}
[
From:http://tw.wingwit.com/Article/program/net/201311/15094.html