日期:2014-05-17 浏览次数:20784 次
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
//创建数据集
DataSet ds = new DataSet();
//加入数据表
ds.Tables.Add("Person");
//获得数据表
DataTable dtPerson = ds.Tables["Person"];
//创建列的结构
dtPerson.Columns.Add("psnNo", typeof(string));
dtPerson.Columns.Add("psnName", typeof(string));
dtPerson.Columns.Add("psnSex", typeof(string));
dtPerson.Columns.Add("psnAge", typeof(int));
dtPerson.Columns.Add("psnAddress", typeof(string));
//加入两行数据
object[] r1 = new object[] { "001", "zengkun", "man", 23, "xiangtan" };
DataRow r2 = dtPerson.NewRow();
r2["psnNo"]="002";
r2["psnName"] = "ljy";
r2["psnSex"] = "weman";
r2["psnAge"] = 22;
r2["psnAddress"] = "zhuzhou";
dtPerson.Rows.Add(r1);
dtPerson.Rows.Add(r2);
//修改某行数据
DataRow cr = dtPerson.Rows[0];
cr["psnAddress"] = "yiyang";
//获取第二行psnName的数据
string name = ds.Tables["Person"].Rows[1].Columns["psnName"].ToString();
string address = ds.Tables["Person"].Rows[1].Columns["psnAddress"].ToString();
//遍历数据表里面的全部数据
foreach (DataRow r in dtPerson.Rows)
{
foreach (DataColumn c in dtPerson.Columns)
{
Console.Write(" {0}",r[c.ColumnName]);
&