VB和数据库
- 格式:doc
- 大小:657.33 KB
- 文档页数:23
操作MySQL数据库方法1.连接数据库1)下载Connector/Net:/downloads/connector/net/2)安装Connector/Net.3)在工程中,选择Project->Add Reference…菜单,添加MySQl.data 引用,如下图所示:4)在需要连接数据的文件中添加一行,以打开namespace:Imports MySql.data.MySqlClient5)创建一个数据库连接m_strConnection ="server=127.0.0.1;uid=root;pwd=12345;database=computer; pooling=false"m_SqlConn = New MySqlConnection(m_strConnection)2.显示数据1)创建一个数据库适配器Dim strQuery As String = "select * from Product"Dim daAdapter As MySqlDataAdapter = New MySqlDataAdapter(strQuery, m_SqlConn)2)创建一个Datatable,用数据库适配器来填充datatableDim dsTable As DataTable = New DataTable(strDataset)daAdapter.Fill(dsTable)3)绑定datatable到DataGrid控件dbGridShow.SetDataBinding(dsTable, "")dbGridShow.Visible = True3.获取表中的某一行某一列‘获得指定行Dim CurRow As IntegerCurRow = dbGridShow.CurrentRowIndexDim tbl As DataTable = dbGridShow.DataSourceDim Row As DataRow = tbl.DefaultView(CurRow).Row'根据当前选定的行得到Delete操作的SQL语句Dim strDelete As StringstrDelete = "delete from T_Bars where name = '" + Row("name") + "'"4.修改或删除数据'定义操作的SQL语句Dim strSql As StringstrSql = "insert into Product(maker,model,type) values('"…”)'创建SqlCommand对象Dim myCommand As MySqlCommand = New MySqlCommand(strInsertCommand, m_SqlConn) ‘执行操作m_SqlConn.Open()nRows = myCommand.ExecuteNonQuery()。
如何设置对vb数据库连接的动态路径_visualbasic教程西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!我个人因为经常作一些数据库方面的程序,对于程序间如何与数据库进行接口的问题之烦是深有体会,因为VB 在数据库链接的时候,一般是静态,即数据库存放的路径是固定的,如用VB的DATA,adodc,DataEnvironment 等到作数据库链接时,如果存放数据库的路径被改变的话,就会找不到路经,真是一个特别烦的事。
笔者的解决方法是利用app.path来解决这个问题。
一、用data控件进行数据库链接,可以这样:在form_load()过程中放入:privateform_load()DimstrAsString定义str=App.PathIfRight(str,1)<>"\"Thenstr=str "\"EndIfdata1.databasename=str&"\数据库名"data1.recordsource="数据表名"data1.refreshsubend这几句话的意为,打开当前程序运行的目录下的数据库。
你只要保证你的数据库在你程序所在的目录之下就行了。
二、利用adodc(ADODataControl)进行数据库链接:privateform_load()DimstrAsString定义str=App.PathIfRight(str,1)<>"\"Thenstr=str "\"EndIfstr="Provider=Microsoft.Jet.OLEDB.3.51;PersistSecurityInf o=False;DataSource="&str&"\tsl.mdb"Adodc1.ConnectionString=strmandType=adCmdTextAdodc1.RecordSource="select*fromtable3"Adodc1.Refreshendsub三、利用DataEnvironment进行数据库链接可在过程中放入:OnErrorResumeNextIfDataEnvironment1.rsCommand1.State<>adStateClosedTh enDataEnvironment1.rsCommand1.Close如果打开,则关闭EndIfi=InputBox("请输入友人编号:","输入")Ifi=""ThenExitSubDataEnvironment1.Connection1.OpenApp.Path&"\userdata base\tsl.mdb"DataEnvironment1.rsCommand1.Open"select*fromtable3wh ere编号="&i&""SetDataReport2.DataSource=DataEnvironment1 DataReport2.DataMember="command1"DataReport2.showendsub四、利用ADO(ActiveXDataObjects)进行编程:建立连接:dimconnasnewadodb.connection dimrsasnewadodb.recordsetdimstrstr=App.PathIfRight(str,1)<>"\"Thenstr=str "\"EndIfstr="Provider=Microsoft.Jet.OLEDB.3.51;PersistSecurityInf o=False;DataSource="&str&"\tsl.mdb"conn.openstrrs.cursorlocation=aduseclientrs.open"数据表名",conn,adopenkeyset.adlockpessimistic用完之后关闭数据库:conn.closesetconn=nothing以上代码在VB6,win98上运行通过,如有问题请发邮件给我:作者:张振兴E_mail:xingyun0615@->文章整理:西部数码--专业提供域名注册、虚拟主机服务以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!VB数据库连接技巧-精华(2008-11-19 17:17:00)DAO数据约束控件和RecordSet数据约束控件是指要求与Data 控件连接到一起的控件,如DBGird 等。
VB使用ADODB操作数据库的常用方法ADO常用方法下面是我所掌握的使用ADO对数据库操作的一些常用方法,主要是提供给初学者作为参考,有不对的地方请指正.如有补充不胜荣幸准备工作========Dim conn As New ADODB。
Connection ’创建一个Connection 实例,在这里使用New等于将Dim和Set合并为一段代码执行Dim rs As ADODB。
Recordset ’创建一个Recordset 实例,不使用New 是因为,经常需要重复使用Set,因此没必要在这里使用Dim CnStr As String,Sql As String’创建两个字符串变量分别存放两个集合的SQL语句代码段1、装载数据库(不属于Recordset集合)=============Dim FileNamw$,DbIp$,DbName$, DbUser$,DbPw$'以上5个字符串变量分别表示文件路径和文件名、数据库地址、数据库名、数据操作员用户名、操作员密码FileName = App。
Path & "\’数据库名'”DbIp = ”数据库地址"DbName = ”数据库名"DbUser = "数据操作员用户名”DbPw = "操作员密码”'以上变量根据数据库类型的不同而不同,有可能只需要1至两个变量’1)连接Access数据库:'-—-----——-——-——-—--CnStr = "PROVIDER=microsoft。
jet.oledb。
3.51;persist security info =false;data source="& FileName & ”;Jet OLEDB:Database Password="&DbPw’2)连接Oracle数据库:'--—-—---—-——-——-——-CnStr = "PROVIDER=MSDAORA。
附录VB And Database1 VB Database Access MethodVB database visit many ways, under normal circumstances is divided into two categories, and each category there are many ways. Below in tabular form for each of the categories, and each category contains methods.1.1Object Interface (API)VBSQL : through DB-Library visit the Microsoft SQL Server ODBC API : any ODBC data source 16 and 32 DAO / Jet : Jet local / Access.MDB. Index sequence database (ISAM) and any ODBC data source DAO / ODBC Direct : any ODBC data source (after RDO) RDO 2.0 : any ODBC data source (Level I or II) ADO : any ODBC data source and any OLE DB interface through the data source interface1.2Data Control ActData Control : DAO / Jet Data Interface Control Data Interface / Direct ODBC : DAO / ODBC data interface Direct Control Interface RemoteData / RD C : RDO data interface Advanced Data Interface Connector / ADC : ADO data interface Interface2 Several of InterfaceADC (Advanced Data Connector) : Advanced Data Connector. ADO provide bundled data source window to the data binding controls on. ADC is essentially a visit or through direct access remote ADO OLE DB object of a technology, It also supports major applications in the Microsoft IE browser bundled with the data control. It is specifically for the Web browser-based application designed.ADO (Active Data Objects) : Active Data Objects. Yes DAO / RDO the successor product, ADO "expansion" of the DAO and RDO used by the object model, This means that it contains fewer targets, more attributes, methods (parameters), and the incident. ADO is a real visit with the various types of data linking mechanism. ADO designed for a very simple format, through the ODBC interface with the database. Can use any ODBC data source, that is not suitable for SQL Server, Oracle, Access such as database applications, but also for Excel spreadsheets, text files, No graphics files and the data file format is an easy-to-use application program interface layer. ADO isMicrosoft's latest and most powerful data access model designed OLE DB, for any OLE DB data source to provide a high-performance interviews, including data sources and non-relational databases, E-mail and file systems, text and graphics, custom business objects, and so on. ADO in key Internet program for at least the network traffic, and the front-end data sources and the use of at least between the layers, all of which are meant to provide light volume, high-performance interface. So ADO through its internal attributes and methods of providing unified data access interface method.DAO (Data Access Objects) : data access object. Is an object-oriented inte rface to the interface. Through DAO / Jet functi on can access ISAM database, use DAO / ODBC Direct functions can be remote RDO function. DAO use the program code is very simpl e. DAO provide rich cursor (Cursor) type and the result of non-cursor (Cursor-Less) type of res ult sets, with the DDL (data description language) is ver y similar to the function. DAO through ODBC, as a direct link to other databases. connect directly to the Access database. DAO sy stem suitable for single applications or small - scale use of the local distribution. DAO model is to design relational database system archit ecture object class collection. They provide a complete management system for the entire oper.html ation of the properties and methods. including the creation of the database, definition of the tables, the fields and indexes, the establishment of the relationship between the tables, positioning and searching tools such as databa ses.JET (Joint Engine Technology) : data connectivity engine technologies. A workstation is based on the adoption of DAO database access mechanism. Although Microsoft Access can be provided by the ODBC driver visit Jet database However, the use of these drivers functional restrictions. Jet has its own mechanism for the inquiry and the result set handling, and can be the same species or dissimilar data sources for query processing.0DBC (Open Database Connectivity) : open database connectivity. Heterogeneous database for the visit provides a unified interface. ODBC based on SQL (Structured Query Language), and regarded it as the standard database. This interface provides maximum interoperability : an application program through a group of generic code access different database management systems. A software developer in the development of client / server applications will not be binding on a particular database above. ODBC for different database for a corresponding driver, is a recognized source of the data Interface. It fast and light to provide a unified interface and the interface to any ODBC data source optimized for neither.ODBC API : manufacturers to the database designer procedures provide direct access to the database of a group function. Note : The point here is that although the ODBC API provides a lot of convenience and powerful functionality. But usually the ODBC API are relatively hard to learn, but it is very prone to error. Although ODBC API allowed to operate ODBC handle, but they still have to be careful If incorrect use ODBC API, it may result in unpredictable error. For example, if the use of ODBC API code to close the connection or ODBC release handle of any one, Then RemoteData control or RDO behavior will be unpredictable. ODBC handle depositary for the reserve is meaningless, because they are variable.ODBC Direct : DAO is a target based on the new DAO mode its methods and attributes and RDO function the same. Use the DAO code of occasions, can be used to access remote data sources.OLE DB : COM model is the database interface. The bottom is a data access interface interface. Is the third driver for the development of businesses exporting data sources to ADO applications, or for C + + developers development of customized database components. It will be able to handle any type of data. OLE DB application to provide a uniform data access methods, without considering their format and storage. In practical application, which means you can visit diversity presence in the electronic data sheets, text files and even mail server Microsoft Exchang such as the data. OLE DB was not directly call VB.RDC (RemoteData Control) : Remote Data Access Control. RDO right is a bundled component data. Can output the result of specific data sources to control.RDO (Remote Data Objects) : Remote Data Objects. ODBC is one of the object-oriented data access interface, Remote data objects and code sets for use to create and operate a remote ODBC database system for the various components a framework. Set targets and have a database describing the characteristics of the various components used to manipulate the attributes and methods of these components. Can set targets and establish relations between these relations on behalf of the database system, the logical structure. It's easy to use the same style DAO combination provides an interface, ODBC form displayed all the bottom function and flexibility. RDO ODBC API is a shallow interfacing. Is designed to access remote data sources ODBC relations and design. Although the visit RDO Jet or ISAM database is restricted, but it can only work through the existing ODBC driver to access the relational database. However, the RDO has been proved to be many SQL Server, Oracle and other major database developers often choose the best interface. RDO provided to access storedprocedures and complex set of results more and more complex objects, attributes and methods.VBSQL : Visual Basic is structured query language. An API is the interface, almost C DB-Library API the same. VBSQL only support Microsoft's SQL Server. VBSQL fast and light but not for the target interface.3Performance Comparison and Application NoteVB-based SQL Server database system Several visits over the SQL Server specific features. DAOs is based on the object, so it is easy to use, However it from Visual Basic to SQL Server slowest way. ODBC API and VBSQL method is essentially based on the procedures. ODBC API method is common, allow the strongest interoperability, a simple programming, However, the slow pace in VBSQL methods. VBSQL VBSQL Control Methods, provided an important SQL Server front-end applications needed flexibility, strong functions and good performance. It is truly event-driven and error handling capability, fully supports asynchronous processing, and pointing out such calculations. These methods are VBSQL other methods beyond the edge, but its slightly complex programming. RDO ODBC API is located at the top of a layer object model, which rely on ODBC API, ODBC driver and the back-end database engine to achieve. RDO used in the procedures necessary for the short (about 250 KB), fast. RDO equipped with the basic approach of ODBC, most direct enforcement ODBC API function, RDO 4.0/VB included in the VB 5.0 Enterprise Edition, MSRDO32.DLL by Dynamic Link Library is to be achieved. RDO is a synthesis of DAO / Jet, VBSQL / DBLib ODBC and the advantages of the object model, ODBC API includes application layer, designed for in the background (server side) have a database exists on the premise of running, against Oracle and SQL Server specially designed. RDO has the advantage of being fully integrated in VB, can directly access SQL Server stored procedures fully supports T-SQL, T-SQL debugging in the integrated development environment, Visual Database Tools integration, and so on. However, Microsoft has announced that in future no longer be right VBSQL / DBLib upgrade, ODBC API function and general programming method is loved by the people, RDO application will be reduced gradually. As for the actual use of any interface, to a large extent dependent on the user's application specific circumstances. 4VB database principleVB access to the database, it is necessary to analyze specific issues, according to the specific circumstances, conditions, request with the appropriate programs, it should pay attention to the following principles :4.1code reuse and efficiency of the operationFor example : through the use of ODBC data sources connecting to the database, we will be able to transform the various types of databases, instead frequent code changes. VBSQL used by DB-Library will not do. ODBC interface and VB database is not running the most efficient method. Similarly, the use of ADO ODBC interface to the efficiency than RDO.4.2achieve simplicity, ease of maintenanceIf a method to achieve together is very complex, the development will inevitably result in the waste of human and material resources. Moreover, the design application will only support up more complex or more difficult to maintain. For example : local needs ISAM or visit Jet types of data sources, the use of DAO / Jet, and it is not necessary to use the method through ODBC. RDC together to achieve more easily than the RDO.4.3safety principlesThis should be an environment and conditions. For example, the LAN network security better in the WAN thus directly control the use of data such as RDC, This realization together fast and convenient, and WAN needs of a large number of errors caught, As the RDC is not as good as using the wrong RDO easy to control.5Concluding remarksThe data access interface DAO system suitable for single applications or small-scale use of the local distribution, RDO which has been proved to be many SQL Server, Oracle and other major database developers often choose the best interface. ADO DAO / RDO the successor product. ADO 2.0 functional and RDO more similar, but generally speaking, In this model between the two species have a similar relationship mapping. ADO "expansion" of the DAO and RDO by the use of the object model, which means that it contains fewer targets, more attributes, methods and parameters, and the incident.Database Interface is the latest ADO, it is more than DAO and RDO more simple, more flexible object model. For new projects, it should be used as ADO data access interface.However, the ADO is not automatic, and the existing data access application code compatible. When ADO Packaging DAO and RDO the function of the need to change many of the elements of language syntax ADO. In some cases, this will mean that some of the existing code of a function of a simple conversion. In other cases, the best approach might be the use of new functional ADO rewriting the application. Also notethat the current ADO DAO does not support all the functions. ADO include RDO style functionality, and to OLE DB data source interaction, It also includes long-range, and DHTML technologies.Generally speaking, in ADO 1984, Most immediately put DAO application ADO transplant may be premature ADO because the current data do not support the definition (DDL), users, groups. However, if only DAO for client / server applications, and is not dependent on the Jet database engine or not to use DDL, then it could be transplanted to ADO.VB与数据库1 VB访问数据库的方法VB访问数据库的方法很多,一般情况下分成两大类,而每一类又有很多方法。
在专业的数据库软件开发中,为了确保数据库中信息的安全,往往要求对数据库文件进行加密,以防止非法用户通过其它的常规手段将其打开。
那么,在Visual Basic中如何建立与加密的数据库的连接呢?笔者在日常工作中,总结了一些方法和技巧,现写出来与同行们交流。
一、连接加密的Access数据库,假设要连接的数据库为hg.mdb,密码为“123”在Visual Basic 6.0中,要建立与数据库的连接,可采用的技术手段很多,如:数据控件、数据对象、数据环境设计器等。
1、使用控件① Data控件Data控件是Visual Basic 6.0中的一个内置数据控件,可以通过设置Data控件的connect、DatabaseName、RecordSource属性实现对数据库的连接和访问。
通过Data控件连接加密的数据库的方法有两种:一种方法是在设计状态时,在“属性窗口”中将Data控件的connect属性的缺省值”Access”改为”; pwd=123”即可,其它属性的设置方法与没有加密的Access数据库的连接相同。
另一种方法是在运行时,通过代码对connect属性赋值来实现。
如:Data1.connect=”; pwd=123”Data1.DatabaseName=APP.path + “\hg.mdb”其中,”123”为Access数据库文件hg.mdb的密码,下同。
②Adodc控件Adodc控件是一个ActiveX控件,它使用Microsoft ActiveX Data Objects(ADO)创建到数据库的连接。
使用Adodc控件之前,要先将Adodc控件添加到控件工具箱中。
方法如下:在VB 6.0种选择“工程”菜单,再点击“部件”菜单项,在弹出的“部件”对话框中选中“Microsoft ADO Data Control 6.0(OLEDB)”选项即可。
通过Adodc控件连接加密的数据库的方法也有两种:一种方法是在设计状态时,在“属性窗口”中,对Adodc控件的ConnectionString属性设置一个有效的连接字符串,并在连接字符串后增加上”; Jet OLEDB: DataBase password=123”,再设置Adodc控件的CommandType、RecordSource的属性就可以创建到加密的数据库的连接了。
VB和数据库11.1 数据库(DateBase)的基本概念一、概念与术语目前几乎所有的应用程序都需要存放大量的数据,这些大量数据一般都需要经过数据收集、分析后,整理成一定的组织结构,通过数据库管理系统来进行管理。
数据库管理系统提供了数据在数据库内存放方式的管理能力,使程序设计人员在使用数据库时不需要考虑数据的具体操作及数据之间的关系。
数据库是一组排列成易于处理和读取的相关信息的集合。
数据库按其模型结构可分为层次数据库、网状数据库和关系数据库。
其中关系数据库的理论发展最为完备,因此到目前为止关系数据库的应用最为广泛。
Visual Basic默认的数据库为微软的Access数据库,可在Visual Basic中利用数据库管理器直接创建,数据库文件的扩展名为.MDB。
除此之外,Visual Basic还可以处理各种外部数据库,如dBASE、FoxPro、Paradox、Btrieve、Microsoft Excel、Lotus1-2-3、Text等格式的数据库,甚至其它非关系的数据库,VB在调用这些数据时,都会自动将其转换为关系型的数据库来操作。
1.数据表(Table)数据表是一组相关联的按行与列排列的逻辑数据的集合,简称为表。
在数据库理论中常用来表示一个实体,每个数据表必须有一个表名,一个数据库一般由多个数据表组成,各个数据表之间一般应存在某种关系。
2.字段(Field)数据表中的每一列称为一个字段。
数据表是由其包含的所有字段构成的,每个字段用来描述它包含的数据。
在创建数据表时,必须为每个字段起一个名称一般称为字段名,同时需要为每个字段分配一个数据类型、最大长度和其它属性。
3.记录(Record)数据表中的每一行称为一条记录。
记录是字段值的集合,所有用户对数据的访问对象是记录,因此,如果想对某个数据项进行访问,必须首先进行记录定位,严格的说,在一个数据表中不允许存在两条完全相同的记录。
4.关键字(Keyword)如果数据表中某个字段值或若干个字段值的集合能唯一确定一条记录,则称该字段或字段的集合为该数据表的关键字。
在一个数据表中,关键字可能存在多个,但需选定其中一个作为主关键字。
对于数据表中的每条记录来说,主关键字的值必须唯一。
5.索引(Index)为了提高访问数据库的速度,大多数数据库都使用索引。
索引即对数据表中的某个或几个字段的值按某种特定的顺序进行排列。
在VB中利用索引可以加快数据检索速度。
6.结构化查询语言(SQL)SQL(Structure Query Language)是结构化查询语言的缩写,是数据库中通用的数据查询编程语言,它是我们访问数据库的一种利器。
在Visual Basic 中访问数据库时,就可以利用SQL同数据库进行通讯。
二、数据库的设计数据库的设计一般需要借助E- R图进行数据建模,然后把整个E-R图中的每个实体和有关的联系,都转化为关系。
对每个关系再进行规范化,最后给出各个数据表完成数据库的逻辑设计,对每个关系来说,在选定的数据库管理系统下,创建数据库、数据表以及它们之间的关系,并输入数据进行测试,完成数据库的物理设计。
具体内容请读者参考相关的数据库理论方面的书籍。
11.2 Visual Basic的可视数据管理器Visual Basic 所支持的不同类型的数据库大都可以通过相应的数据库管理系统来建立,如在MS Access中可以建立.MDB的数据库。
也可以使用Visual Basic下的数据库管理器来管理数据库。
启动数据库管理器有两种方法:(1)在Visual Basic集成环境中,单击“外接程序”菜单下的“可视化数据管理器”命令,即可打开可视化数据管理器“VisData”窗口,如图11-1所示。
(2)直接运行Visual Basic系统目录中的VisData.exe程序文件来启动可视化数据管理器。
VisData窗口由菜单栏、工具栏、工作区窗口与状态栏组成,刚启动时其工作区窗口为空。
图11.2-1 可视化数据管理器窗口一、可视数据管理器的使用使用开可视化数据管理器建立默认数据库――Access数据库的步骤如下:1.单击“文件”菜单下的“新建”菜单项,将出现如图12-2所示的选择。
单击数据库类型菜单中的Access将出现版本子菜单,在版本菜单中选择要创建的数据库版本后,出现如图11-3所示的窗口。
图11.2-2 “文件”菜单下的“新建”子菜单图11.2-3 创建数据库窗口2.单击“保存”按钮后,将会出现如图11.2-4所示的窗口。
图11.2-4 数据库窗口与SQL语句窗口3.右击数据库窗口,出现如图1.2-5所示的窗口。
图11-5 数据库窗口4.单击“新建表”按钮,即可打开如图11.2-6所示窗口。
即可新建表。
图11.2-6 表结构窗口5.然后单击“添加字段”按钮,可建立各个字段的属性,最后单击“新建表”按钮,即可建立起表结构。
有了表结构后就可对表进行数据的添加、修改和删除等操作。
11.3 ADO数据控件一、编写一个简单数据库程序首先介绍Visual Basic所提供的两个控件ADO Data Control(简称ADODC)和DataGrid处理数据库程序的方法。
ADO Data Control和DataGrid都属于ActiveX控件,它们在使用前需要先将其添加到工具箱中,以便在工程中使用。
单击“工程/部件”菜单项,打开“部件”对话框,选定所需要的控件并确定,即可将其添加的工具箱中,如图11.3-1所示。
图11.3-1 添加ADODC与DataGrid控件到工具箱在窗体上添加ADODC与DataGrid控件,如图11-8所示,然后右击ADODC控件,选择其快捷菜单下“ADODC 属性”则打开如图11-9所示的界面。
图11.3-2 窗体窗口图11.3-3 属性页窗口在上图中,单击生成“按钮”,则打开如图11.3-4所示的数据链接属性窗口。
图11.3-4 数据链接属性窗口在图11.3-4中选中“Microsoft Jet 3.51 OLE DB Provider”,单击下一步按钮,进入如图11.3-5所示的数据链接属性窗口。
【注】Microsoft Jet 3.51OLE DB Provider只能打开Access97下的数据库,不能打开Access2000及以上版本的数据库,如要打开Access2000下的数据库,则必须选用Jet 4.0,否则系统会出错。
图11.3-5 数据链接属性窗口在图11.3-5中,单击“…”按钮,选择所要打开的数据库文件后确定,即可单击“测试连接”按钮,测试是否连接成功。
如果测试连接成功,则单击“确定”按钮后,返回图11.3-2。
【注】如果数据库文件与工程文件保存在同一个文件夹下,则可把上图所示的文件xjgl.mdb前的字符删除,这样生成的程序,在复制到其它计算机上后不需重新对数据库进行连接即可运行。
通过以上步骤,我们只是完成了ADODC控件的ConnectionString属性设置,其值如下:Provider=Microsoft.Jet.OLEDB.3.51;Data Source=e:\xjgl\xjgl.mdb;Persist Security Info=False该属性由三个参数组成:然后把ADODC 控件的CommandType属性设置为adCmdTable,接着单击RecordSource属性,可以看到数据库xjgl.mdb下的所有数据表,单击“基本情况表”。
再选定DataGrid控件,只需把DataSource设置为ADODC 控件即可,完成程序设计。
运行该程序即可对数据库xjgl.mdb下的“基本情况表”进行浏览。
然后把DataGrid控件的AllowAddNew属性设置为True、AllowDelete属性设置为True、AllowUpdate属性设置为True,再次运行该程序,即可对“基本情况表”进行添加、删除与修改数据操作,到目前为止没有书写一行代码,就已完成程序设计。
二、数据库文件与ADODC、DataGrid的关系1.ADODC与数据库文件的关系数据库文件是一种格式十分复杂的文件,对任何一个程序员来说,如果想自行解析文件格式进而存取数据库中的内容,都是一件十分困难的事情。
因此VB提供了ADODC控件,其功能是进行数据库文件结构分析,并且根据程序将来可能操作数据库(如:读取、写入、新增、删除等),建立必要的操作接口(包含属性、方法和事件),然后封装成为一个控件。
对数据库程序而言,如果通过ADODC控件来存取数据,则不必了解数据库文件格式,只要通过ADODC所提供的操作接口,即可达到存取数据库的目的。
数据库程序、ADODC控件与数据库文件三者之间的关系用下图表示:ADODC图11.3-6 数据库文件与ADODC控件的关系2.DataGrid与ADODC之间的关系虽然ADODC控件具有存取数据库数据的能力,但却没有提供显示数据的功能,如果想要显示数据库的内容,除了利用ADODC来读取数据之外,还得编写显示数据的程序。
1)利用DataGrid来显示数据要使用DataGrid 来显示数据库内容必须设置好它的DataSource属性,这个属性的作用是让DataGrid控件与ADODC控件产生连接关系,也就是告诉DataGrid控件它所存取的是哪一个ADODC所打开的数据库,接着DataGrid就会自动使用ADODC的接口,将数据库的内容显示出来。
其关系如图11.3-7:数据库文件数据库程序图11.3-7 DataGrid 与ADODC 控件的关系 2)关键属性和参数总结ADODC 控件的属性及参数:(1)ConnectionString(连接字符串)属性:该属性有两个重要参数Provider 和Data Source 。
Provider 参数:Provider 是OLE DB Provider 的简称,其主要用途在于指定存取数据库所使用的OLE DB 驱动程序。
如“Microsoft Jet 3.51 OLE DB Provider”。
Data Source (数据源)参数:此参数通常用来指定数据库文件的完整路径和文件名。
有了此参数,ADODC 控件才能打开指定的数据库文件。
(2)CommandType(命令类型)属性:共有四个值:adCmdTable(数据表)、adCmdText (指令)、adCmdStoredProc (存储过程)、adCmdUnknown (未定义)。
(3)RocordSoure(记录源)属性:指定确定的表。
DataGrid 控件的属性:(1)DataSource(数据源)属性:由于DataGrid 控件并不直接存取数据库中的数据,因此必须将其属性设置成某一个ADODC 控件,DataGrid 控件才能具备存取数据库的能力。