当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

VB.Net - 数据库访问

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:59:14

由 yiyohunter 创建,youj 最后一次修改 2016-12-12 应用程序与数据库通信,首先,检索存储在那里的数据,并以用户友好的方式呈现它,其次,通过插入,修改和删除数据来更新数据库。Microsoft ActiveX Data Objects.Net(ADO.Net)是一个模型,.Net框架的一部分,由.Net应用程序用于检索,访问和更新数据。 ADO.Net对象模型 ADO.Net对象模型只不过是通过各种组件的结构化流程。 对象模型可以被图形描述为: 通过数据提供者检索驻留在数据存储或数据库中的数据。 数据提供者的各种组件检索应用程序的数据并更新数据。 应用程序通过数据集或数据读取器访问数据。 Datasets 数据集:数据集将数据存储在断开连接的缓存中,应用程序从中检索数据。 Data readers 数据读取:数据读取器以只读和仅转发模式向应用程序提供数据。 数据提供程序数据提供程序用于连接到数据库、 执行命令和检索数据,将其存储在一个数据集,阅读检索到的数据和更新数据库。在 ADO.Net 数据提供程序包括以下四个对象︰ SN对象和说明1 Connection This component is used to set up a connection with a data source.该组件被用来建立与数据源的连接。 2 Command A command is a SQL statement or a stored procedure used to retrieve, insert, delete or modify data in a data source.命令是用于检索,插入,删除或修改数据源中的数据的SQL语句或存储过程。 3 DataReaderData reader is used to retrieve data from a data source in a read-only and forward-only mode.数据读取器用于以只读和仅转发模式从数据源检索数据。 4 DataAdapterThis is integral to the working of ADO.Net since data is transferred to and from a database through a data adapter. It retrieves data from a database into a dataset and updates the database. When changes are made to the dataset, the changes in the database are actually done by the data adapter.这是ADO.Net的工作的组成部分,因为数据通过数据适配器传输到数据库和从数据库传输。 它将数据从数据库检索到数据集并更新数据库。 当对数据集进行更改时,数据库中的更改实际上由数据适配器完成。 ADO.Net中包含以下不同类型的数据提供程序 SQL Server的.Net Framework数据提供者 - 提供对Microsoft SQL Server的访问。 OLE DB的.Net Framework数据提供者 - 提供对使用OLE DB公开的数据源的访问。 ODBC的.Net Framework数据提供程序 - 提供对ODBC公开的数据源的访问。 Oracle的.Net Framework数据提供程序 - 提供对Oracle数据源的访问。 EntityClient提供程序 - 允许通过实体数据模型(EDM)应用程序访问数据。 数据集 DataSet是数据的内存表示。 它是从数据库检索的断开连接的高速缓存的记录集。 当与数据库建立连接时,数据适配器创建数据集并在其中存储数据。 在检索数据并将其存储在数据集中之后,将关闭与数据库的连接。 这被称为“断开连接的架构”。 数据集用作包含表,行和列的虚拟数据库。下图显示了数据集对象模型: DataSet类存在于System.Data命名空间中。 下表描述了DataSet的所有组件: SN组件及说明1 DataTableCollectionIt contains all the tables retrieved from the data source.它包含了从数据源中检索的所有表。 2 DataRelationCollection It contains relationships and the links between tables in a data set.它包含数据集中的表之间的关系和链接。 3 ExtendedProperties It contains additional information, like the SQL statement for retrieving data, time of retrieval, etc.它包含的其他信息,例如用于检索数据的SQL语句,检索的时间等4 DataTable It represents a table in the DataTableCollection of a dataset. It consists of the DataRow and DataColumn objects. The DataTable objects are case-sensitive.它表示数据集的DataTableCollection中的表。它由DataRow和DataColumn对象组成。 DataTable对象区分大小写。 5DataRelation It represents a relationship in the DataRelationshipCollection of the dataset. It is used to relate two DataTable objects to each other through the DataColumn objects.它表示数据集的DataRelationshipCollection中的关系。它用于通过DataColumn对象将两个DataTable对象相互关联。 6 DataRowCollection It contains all the rows in a DataTable.它包含DataTable中的所有行。 7 DataView It represents a fixed customized view of a DataTable for sorting, filtering, searching, editing and navigation.它表示用于排序,过滤,搜索,编辑和导航的DataTable的固定自定义视图。 8 PrimaryKey It represents the column that uniquely identifies a row in a DataTable.它表示唯一标识DataTable中某一行的列。 9DataRow It represents a row in the DataTable. The DataRow object and its properties and methods are used to retrieve, evaluate, insert, delete, and update values in the DataTable. The NewRow method is used to create a new row and the Add method adds a row to the table.它表示DataTable中的一行。 DataRow对象及其属性和方法用于检索,评估,插入,删除和更新DataTable中的值。 NewRow方法用于创建一个新行,Add方法向表中添加一行。 10 DataColumnCollection It represents all the columns in a DataTable.它表示DataTable中的所有列。 11DataColumn It consists of the number of columns that comprise a DataTable.它由组成DataTable的列数组成。 连接到数据库 .Net框架提供两种类型的Connection类: SqlConnection -设计用于连接到Microsoft SQL Server。 OleDbConnection -设计用于连接到各种数据库,如Microsoft Access和Oracle。 示例1 我们有一个表存储在名为testDB的数据库中的名为Customers的Microsoft SQL Server中。 有关在SQL Server中创建数据库和数据库表的信息,请参考“SQL Server”教程。让我们连接到此数据库。 执行以下步骤: 选择工具 - >连接到数据库在“添加连接”对话框中选择服务器

[1] [2]  下一页


VB.Net - 数据库访问