编码实现>微软数据访问技术ADO.NET>使用DataSet>
编码实现>微软数据访问技术ADO.NET>使用DataSet>了解DataSet
DataSet就是一个内存数据库,DataSet内部以XML表示数据,不仅可以是数据库中数据的副本,还可以从XML,CSV文件导入记录。
DataSet的层次关系图
编码实现>微软数据访问技术ADO.NET>使用DataSet>使用DataTable实现内存表
使用DataTable实现内存表
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MemoryTable
{
class Program
{
static void Main(string[] args)
{
//为DataTable指定一个表名,通过dt.TableName可以访问表名。
DataTable dt = new DataTable("内存表示例");
//为DataTable添加架构信息,也就是创建字段。
dt.Columns.Add(new DataColumn("书名", typeof(string)));
dt.Columns.Add(new DataColumn("书号", typeof(string)));
DataColumn[] dcs = new DataColumn[2];
dcs[0] = new DataColumn("价格", typeof(decimal));
dcs[1] = new DataColumn("出版社", typeof(string));
dt.Columns.AddRange(dcs);
DataRow dr = dt.NewRow();
dr["书名"] = "C#编程系列";
dr["书号"] = "12345-678-90";
dr["价格"] = 45.3;
dr["出版社"] = "我的出版社";
dt.Rows.Add(dr);
dt.Rows.Add(new object[] { "C#编程系列二", "33455-333-333", 45.7, "我的出版社" });
DisplayResult(dt);
Console.ReadLine();
}
static void DisplayResult(DataTable dt)
{
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
Console.Write(dt.Columns[i].ColumnName.PadRight(10));
}
Console.WriteLine();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
foreach (DataColumn col in dt.Columns)
{
Console.Write(dt.Rows[i][col].ToString().PadRight(10));
}
Console.WriteLine();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MemoryTable
{
class Program
{
static void Main(string[] args)
{
//为DataTable指定一个表名,通过dt.TableName可以访问表名。
DataTable dt = new DataTable("内存表示例");
//为DataTable添加架构信息,也就是创建字段。
dt.Columns.Add(new DataColumn("书名", typeof(string)));
dt.Columns.Add(new DataColumn("书号", typeof(string)));
DataColumn[] dcs = new DataColumn[2];
dcs[0] = new DataColumn("价格", typeof(decimal));
dcs[1] = new DataColumn("出版社", typeof(string));
dt.Columns.AddRange(dcs);
DataRow dr = dt.NewRow();
dr["书名"] = "C#编程系列";
dr["书号"] = "12345-678-90";
dr["价格"] = 45.3;
dr["出版社"] = "我的出版社";
dt.Rows.Add(dr);
dt.Rows.Add(new object[] { "C#编程系列二", "33455-333-333", 45.7, "我的出版社" });
DisplayResult(dt);
Console.ReadLine();
}
static void DisplayResult(DataTable dt)
{
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
Console.Write(dt.Columns[i].ColumnName.PadRight(10));
}
Console.WriteLine();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
foreach (DataColumn col in dt.Columns)
{
Console.Write(dt.Rows[i][col].ToString().PadRight(10));
}
Console.WriteLine();
}
}
}
}
编码实现>微软数据访问技术ADO.NET>使用DataSet>把数据库中的数据填充到DataSet,以及访问DataSet中的数据
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace FillDataSet
{
class Program
{
static void Main(string[] args)
{
const string ConnectionStr = @"Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|\Northwnd.mdf;Integrated Security=True;user Instance=True;";
using (SqlConnection conn = new SqlConnection(ConnectionStr))
{
try
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter daOrders = new SqlDataAdapter("Select top 2 * from orders", conn);
daOrders.Fill(ds,"orders");
Console.WriteLine("填充Orders表到DataSet成功");
SqlDataAdapter daProducts = new SqlDataAdapter("Select top 2 * from Products", conn);
daProducts.Fill(ds,"Products");
Console.WriteLine("填充Products表到DataSet成功");
SqlDataAdapter daOrderDetails = new SqlDataAdapter("Select top 2 * from [Order Details]", conn);
daOrderDetails.Fill(ds,"Order Details");
Console.WriteLine("填充Order Detail表到DataSet成功");
Console.WriteLine("访问Orders表的第一行第一列记录值是:");
Console.WriteLine(ds.Tables[0].Rows[0][0]);
//使用CreateTableReader方法显示所有的数据
DisplayResult(ds);
}
catch (SqlException ex)
{
Console.WriteLine("在处理数据的过程中发生了错误:{0}", ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
Console.Read();
}
static void DisplayResult(DataSet ds)
{
using (DataTableReader dtr = ds.CreateDataReader())
{
do
{
if (dtr.HasRows)
{
while (dtr.Read())
{
for (int i = 0; i < dtr.FieldCount; i++)
{
Console.Write(dtr[i] + " ");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("没有数据");
}
Console.WriteLine("---------------------" + Environment.NewLine);
} while (dtr.NextResult());
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace FillDataSet
{
class Program
{
static void Main(string[] args)
{
const string ConnectionStr = @"Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|\Northwnd.mdf;Integrated Security=True;user Instance=True;";
using (SqlConnection conn = new SqlConnection(ConnectionStr))
{
try
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter daOrders = new SqlDataAdapter("Select top 2 * from orders", conn);
daOrders.Fill(ds,"orders");
Console.WriteLine("填充Orders表到DataSet成功");
SqlDataAdapter daProducts = new SqlDataAdapter("Select top 2 * from Products", conn);
daProducts.Fill(ds,"Products");
Console.WriteLine("填充Products表到DataSet成功");
SqlDataAdapter daOrderDetails = new SqlDataAdapter("Select top 2 * from [Order Details]", conn);
daOrderDetails.Fill(ds,"Order Details");
Console.WriteLine("填充Order Detail表到DataSet成功");
Console.WriteLine("访问Orders表的第一行第一列记录值是:");
Console.WriteLine(ds.Tables[0].Rows[0][0]);
//使用CreateTableReader方法显示所有的数据
DisplayResult(ds);
}
catch (SqlException ex)
{
Console.WriteLine("在处理数据的过程中发生了错误:{0}", ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
Console.Read();
}
static void DisplayResult(DataSet ds)
{
using (DataTableReader dtr = ds.CreateDataReader())
{
do
{
if (dtr.HasRows)
{
while (dtr.Read())
{
for (int i = 0; i < dtr.FieldCount; i++)
{
Console.Write(dtr[i] + " ");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("没有数据");
}
Console.WriteLine("---------------------" + Environment.NewLine);
} while (dtr.NextResult());
}
}
}
}
DataView对象比DataTable对象更多的功能,通过使用DataView对象,可以实现对数据表的排序,搜索,帅选等功能。
合乎自然而生生不息。。。