DataSet是ADO.NET中的核心对象. DataSet包含一组DataTable对象.
每个DataTable对象都有一些子对象DataRow和DataColumn, 表示数据库表中的行和列. 通过这些对象可以获取表,行和列中的所有元素.
DataSet的常见操作是用DataAdapter对象的Fill()方法给它填充数据.
访问DataSet中的表:
1. 按表名访问: thisDataSet.Tables["Customers"] 指定DataTable对象Customers
2. 按索引(索引是基于0的)访问: thisDataSet.Tables[0] 指定DataSet中的第一个DataTable.
访问DataTable的行和列:
1. 在每个DataTable中,都有一个Rows属性, 它是DataRow对象的集合.
myDataSet.Tables["Customers"].Rows[n]
在thisDataSet的DataTable对象Customers中指定行号n-1(索引是基于0的).
2. DataRow对象有一个重载的索引符属性, 允许按列名或列号访问各个列.
thisDataSet.Tables["Customers"].Rows[n]["CompanyName"]
实例:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataSetReader
{
class Program
{
static void Main(string[] args)
{
//string connstring = @"Data Source=ZHANG-1D093B228;Initial Catalog=northwind;User ID=sa;Password=sa";
//string connstring = @"Server=.;Integrated Security=True; Database=northwind";
//string connstring = @"Server=ZHANG-1D093B228;Integrated Security=True; Database=northwind";
string connstring = @"Server=localhost;Integrated Security=True; Database=northwind";
SqlConnection thisconnection = new SqlConnection(connstring );
string strsql=@"select customerID,ContactName from Customers";
SqlDataAdapter thisAdapter = new SqlDataAdapter(strsql, thisconnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Customers");
foreach (DataRow therow in thisDataSet.Tables["Customers"].Rows)
{
Console.WriteLine(therow["CustomerID"] + "\t" + therow["ContactName"]);
}
thisconnection.Close();
Console.WriteLine("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
}
}
这几种数据库连接方法都可以.
每个DataTable对象都有一些子对象DataRow和DataColumn, 表示数据库表中的行和列. 通过这些对象可以获取表,行和列中的所有元素.
DataSet的常见操作是用DataAdapter对象的Fill()方法给它填充数据.
访问DataSet中的表:
1. 按表名访问: thisDataSet.Tables["Customers"] 指定DataTable对象Customers
2. 按索引(索引是基于0的)访问: thisDataSet.Tables[0] 指定DataSet中的第一个DataTable.
访问DataTable的行和列:
1. 在每个DataTable中,都有一个Rows属性, 它是DataRow对象的集合.
myDataSet.Tables["Customers"].Rows[n]
在thisDataSet的DataTable对象Customers中指定行号n-1(索引是基于0的).
2. DataRow对象有一个重载的索引符属性, 允许按列名或列号访问各个列.
thisDataSet.Tables["Customers"].Rows[n]["CompanyName"]
实例:


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataSetReader
{
class Program
{
static void Main(string[] args)
{
//string connstring = @"Data Source=ZHANG-1D093B228;Initial Catalog=northwind;User ID=sa;Password=sa";
//string connstring = @"Server=.;Integrated Security=True; Database=northwind";
//string connstring = @"Server=ZHANG-1D093B228;Integrated Security=True; Database=northwind";
string connstring = @"Server=localhost;Integrated Security=True; Database=northwind";
SqlConnection thisconnection = new SqlConnection(connstring );
string strsql=@"select customerID,ContactName from Customers";
SqlDataAdapter thisAdapter = new SqlDataAdapter(strsql, thisconnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "Customers");
foreach (DataRow therow in thisDataSet.Tables["Customers"].Rows)
{
Console.WriteLine(therow["CustomerID"] + "\t" + therow["ContactName"]);
}
thisconnection.Close();
Console.WriteLine("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
}
}
这几种数据库连接方法都可以.