ADO.NET 基础知识

1、与ODBC,OLE DB。ADO 可访问非关系DB多种接口,可访问关系型数据库。

2、ODBC提供了一组对数据库访问的标准API(应用程序编程接口),处理底层数据,让高层不用例会那种数据库接口。ODBC的数据源就是Access、MSSQl、Oracle、MYSQL

3、ODBC就是一种分层思想,OLE DB操作ODBC
OLE DB标准时实现一组C++API函数,OLE DB符合COM标准、基于对象的。

4、(类似DAO和RDO)ADO是一种对象模型,实现与语言无关,ADO出现了OLEDB标准的API事C++API,只能提供C++语言调用。****************ADO是对oledb数据源存取API的封装,底层API效率好,不易使用,封装越高越容易使用。

5、ADO和ADO.NET是两种数据访问方式
*****************************显著区别是后者全面支持XML数据呈现
*****************************ADO.NET是托管代码库,提供简单灵活的架构
ADO使用OLE DB接口并基于微软的COM技术(COM是开发软件组件的一种方法,一些小的二进制可执行程序,COM组件是符合COM规范的小组件)
ADO.NET拥有自己的ADO.NET接口并基于.NET体系,基于XML格式,不需要再做COM编排导致的数据类型转换。

6、System.Data.SqlClient;
System.Data.Linq;

7、Connection;Command使用对象向数据源发出命令;
ConmmandBuilder用于生成用于更新sql的语句,不必自己创建;(只能更新一个表,表中必须设置 主键,如果数据集是多表关联,则不可用此方法自动更新)

用法:有时候需要缓存的时候,进行一系列的操作最后一并提交数据库 ************************************************************************************
DataReader;轻量级,读取只能向前和只读的数据流
DataAdapter;执行对数据源的各种操作

8、DataSet(DataTable(DataRow,DataColumn),DataRelation)

9、SqlDataReader thisReader=thisCommand.ExcuteReader();
while(thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",thisReader["CustomerID"],thisReader["CompanyName"]);
}

10、Oracle using System.Data.OracleCline;
Access using System.Data.OleDb;
无 using System.Data.Odbc;

11、DataSet对象只有一个Tables属性,填充了后才才能访问行列
myDataSet.Tables["Customrs"].Rows[n]["CompanyName"];

12、SqlDaraAdapter一般用于查询,使用的时候不用con.open(),直接将数据fill到dataset中
SqlCommand 一般用于非查询,也可以用于查询。需要con.open(),使用完要关闭

13、SqlDataAdapter thisAdapter=new SqlDataAdpter("Select CutomerID,CompanyName From Customers",thisConnection);
SqlCommandBuilder thisBuilder=new SqlCommandBuilder(thisAdapter);
DataSet thisDataSet=new DataSet();
thisAdapter.Fill(thisDataSet,"Customers");
---------------------------------------填充完了DataSet后就可以访问其中的行和列了
Console.WriteLine("name before change:{0}",thisDataSet.Tables["Customer"].Rows[9] ["CompanyName"]);

14、DataRow thisRow=thisDataSet.Tables["Customer"].NewRow();
thisRow["CustomerID"]="ZACIZ";
thisRow["CompanyName"]="Zachary Ztither ltd";
thisDataSet.Tables["Customers"].Rows.Add(thisRow);
Console.WriteLine("#rows after change:{0}",thisDataSet.Tables ["Customers"].Rows.Count);

15、DataSet是内存中非连接的数据副本,DataAdapter负责连接到磁盘上的数据库,因此需要调用它的
Update()方法才能使DataSet中的内存数据与磁盘上的数据同步。

16、DataTable对象Row集合提供了Find(); 来检查要添加的行是否已经存在,若为空则进行更改。

???????????????????????????????????????
17、Delete();方法不用来删除行,仅仅用来标记将要删除的行。

18、DataRelation用于描述DataSet中的多个DataTables对象的关系。每个对象都包含DataRelations的Relations集合,以查找和操纵相关的表。

19、DataSet中访问多个表
-----------------------------------------------------------------------------------
DataSet thisDataSet=new DataSet();
SqlDataAdapter custAdapter=new SqlDataAdapter("Select * from Customers",thisConnect);
SqlDataAdapter orderAdapter=new SqlDataAdapter("Select *form Orders",thisConnect);
custAdapter.Fill(thisDataSet,"Customers");
orderAdapter.Fill(thisDataSet,"Orders");
-----------------------------------------------------------------------------------
DataRelation custOrderRel=thisDataSet.Relations.Add("Customer",
thisDataSet.Tables["Customer"].Columns["CustomerID"]
thisDAtaSet.Tables["Customer".Columns["CustomerID"]]);//创建DataRelation并连到DataSet

20、ADO.NET中XML主要集中于DataSet对象,因为XML主要关注的是关系和分层的结构话数据。
DataSet中有7个用于处理XML的方法,最容易使用的是WriteXml();
thisDataSet.WriteXml("nwinddata.xml");
ReadXml()方法也可以用于将XML文件的内容读取到DataSet中

21、ExecuteScalar()要从sql查询中返回一个值
ExecuteNonQuery()类似Insert、Update、Delete的数据修改操作

22、调用存储过程
SqlCommand thisCommand=thisConnection.CreateCommand();
thisCommand.CommandType=CommandType.StoredProduce;
thisCommand.Command.Text="Ten Most Expensive Producets";
SqlDataReader thisReader=thisCommand.ExecuteReader();
while(thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}",thisReader{"TenMostExpensiveProducts",thisReader ["UnitePrice"]});
}
thisReader.Close();
23、ExecuteNonQuery();执行命令,返回影响的行数。int rowsReturned=com.ExecuteNonQuery;
ExecuteReader();执行命令返回一个类型化的IDataReader;
ExecuteScalar();执行命令,返回结果集中第一行第一列的值。
SqlCommand cmd= new SqlCommand(str,con);
object o=cmd.ExecuteScalar();
Console.WriteLine(o);
24、ExecuteXmlReader();只用于SqlClient提供程序
给调用者返回一个XmlReader对象
Using System.Xml;//必须导入命名空间

public static void Main(string [] args)
{
string source="连接字符串";
string select="select * from Works"
SqlConnection conn=new SqlConnetion(source);
conn.open();
SqlCommand cmd=new SqlCommand(select,conn);
XmlReader xr=cmd.ExecuteXmlReader();
xr.Reader();
string data;
do
{
data=xr.ReadOuterXml();
if(!string.IsNullOrEmpty(data))
Console.WriteLine(data);
}while(!string.IsNullOrEmpty(data));
conn.close();
}

25、DataColumn customerID=new DataColumn("CustomerID",typeof(int));
CustomerID.AllowDBNull=false; //设置不可以为空
CustomerID.ReadOnly=false;
CustomerID.AutoIncrement=true; //自动增长
CustomerID.AutoIncrementSeed=1000;// 最初的种子值;递增量默认为1 AutoIncrementStep
//DefaultValue可以定义列的默认值;Expression定义所计算的列中使用的表达式
DataColumn name=new DataColumn("Name",typeof(string));
name.AllowDBNull=false;
name.Unique=true;

26、访问数据行用DataRow来访问,需要引入SqlDataAdapter类
SqlDataAdapter da=new SqlDataAdapter(select,conn);
DataSet ds=new DataSet();
da.Fill(ds,"Customers");
---------------------------使用DataRow类的索引器访问数据行的值,遍历
foreach(DataRow row in ds.Tables["Customers"].Rows)
Console.WriteLine("'{0}'from {1}",row[0],row[1]);
--------------------------------------ds.Tables[表名].Rows[行][列]

27、给数据表添加架构(列),没有包含可空的列,这个仅限于内存,不是添加到数据库中。
DataTable products=new DataTable("Products");
Products.Columns.Add(new DataColumn("ProductID",typeof(int)));
Products.Columns.Add(new DataColumn("ProductName",typeof(int)));
ds.Tables.Add(products);
Products.Columns.Add(new DataColumn("Supplier",typeof(int)));

28、DataRow中的版本功能,可以接收某一特定行上指定列的各个值
Current 列中目前存在的值,如果没有进行编辑,该值与初值相同,如果编辑了,该值就是最后有效值
Default 列的任何默认值设置
Original 最初从数据库中出来的列值,如果调用DataRow类的AcceptChanges方法,该值更新为Current
Proposed

29、fo

posted on 2014-04-07 16:59  钟灵毓秀的家园  阅读(220)  评论(0编辑  收藏  举报

导航