Advertisement
ASP_Volume2 Databases/ Data Access/ DAO/ ADO #31965

Basic Database Programming

Learn how to incorporate Access Databases into your Visual Basic project! Add, delete, edit, and refresh all records in the NWIND.mdb database. Then, display the data from that database in a datagrid and a 3D bar/line/pie graph! Comes with a downloadable example application! Please rate and leave feedback!

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
<html>
<head>
<title>Basic Database Programming</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div align="center"> 
 <p><font size="7" face="Times New Roman">Basic Database Programming</font></p>
 <p><font size="3">-by Donald Evan Campbell</font></p>
 <p> </p>
 <p><font size="5" face="Times New Roman">This tutorial is available at my website! <a href="http://www.geocities.com/campbell_donalde/vb/tutorials.html">Click 
 Here!</a></font></p>
 <p> </p>
 <p align="left"><b><font size="3" face="Times New Roman">What you will learn: </font></b></p>
 <p align="left"><font size="3" face="Times New Roman">This tutorial will teach you the basics of database 
 programming. Learn how to build a connection string, connect to an access 
 database, scroll through the database's entries, add, delete, and edit database 
 entries, and display all the entries and database data in a table-like chart. 
 This tutorial includes links to many different screenshots that will help 
 you along the way to understanding ADO database programming with Microsoft 
 Access. </font></p>
 <p align="left"> </p>
 <p align="left"><b><font size="3" face="Times New Roman">Part One: Obtaining the Underlying Information:</font></b></p>
 <p align="left"><font size="3" face="Times New Roman">Before you begin understanding the system of 
 database programming with access, you must understand basic vocabulary. Below 
 are some listings of words that will provide you with a basis for programming 
 with Access and ADO databases:</font></p>
 <p align="left"> </p>
</div>
<blockquote> 
 <div align="left"> 
 <blockquote> 
 <p><b><font size="3" face="Times New Roman">Connectionstring: </font></b><font size="3" face="Times New Roman">The connectionstring 
 tells the Visual Basic compiler with which system or engine to connect 
 to the database, what database to connect to, and any security issues 
 to handle. You can either write your own, or, like in the example in this 
 tutorial, you can use Visual Basic's own system of building a connection 
 string automatically.</font></p>
 <p><b>Command Type: </b>As you should know, a database includes tables, 
 forms, and text. The Command Type is a property that tells Visual Basic 
 which one of those three it should connect to. In this example, we shall 
 be editing and receiving data from the Customers table, and so Command 
 Type will be set to adCmdTable (the VB compiler should look for a table 
 from which to receive its data.)</p>
 <p><b>ADODC: </b>This is an OCX control for Visual Basic that handles ODBC 
 or Connectionstring connections. We shall be using one of these controls 
 to connect to our database.</p>
 <p><b>ODBC: </b>This is a standard. This standard was invented to work with 
 Microsoft Visual Basic programs, Microsoft Visual C++ programs, and of 
 course, all SQL systems. </p>
 <p><b>OLE DB Provider: </b>These are applications that run databases. They 
 are what interprets your commands in SQL and then sends those commands 
 off to a database which performs your request and spits out a result.</p>
 </blockquote>
 </div>
</blockquote>
<p> </p>
<p><b>Part Two: Starting the Program:</b></p>
<p>Now that you have a basic understanding of the words you will need to know, 
 you can begin the program. Start a normal Standard EXE project in Visual Basic 
 and right click on the toolbar. Select Components.</p>
<p>Scroll down in the components window until you see an entry called: "Microsoft 
 ADO Data Control." Check off the box to this entry's left and click OK. 
 Add an ADODC control to the form wherever you like and resize it to fit your 
 creative needs. Note that its name is ADODC1. Change this name to: "adoConnect."</p>
<p>Now, you shall setup its connectionstring. We shall be looking at the NWIND.mdb 
 file in the Visual Basic folder (c:\program files\microsoft visual studio\vb98\NWIND.mdb 
 is standard) because of its setup. This file, NWIND.mdb is great for learning 
 basic database programming because it was written in Microsoft Access 97, the 
 native version for the ADODC control, and it was written in a business-like, 
 real-world sort of way. This database will give you practice and greatly-needed 
 expertise when it comes to programming business database applications. </p>
<p>To setup the connectionstring, right-click on adoConnect. Select "ADODC 
 Properties." In the window that appears, click on the option box that reads 
 "Use Connectionstring." Click "Build."</p>
<p>This step can get a bit complex to explain. To build a connectionstring, you 
 must tell the computer what PROVIDER you are using. All Microsoft Access databases 
 use the Microsoft Jet OLE DB Provider. If you were running a database from Oracle, 
 you would use their database provider. To select the Microsoft Jet OLE DB Provider, 
 make sure the window that appears after you clicked "Build" is sitting 
 with the "Provider" tab open. There should be a list of providers, 
 and among them should be the Microsoft Jet 4.0 OLE DB Provider. Select this 
 and click on the Connection tab. Now you have to select the database. Click 
 on the "..." button and navigate to your Visual Basic directory. The 
 VB directory, by standard, is "c:\program files\microsoft visual studio\vb98." 
 Once there, click on the NWIND icon and click "Open." The NWIND database 
 is NOT passworded, so you can leave the textboxes alone. If you want to do this, 
 however, with a database you create and password, you would have to add your 
 administrative username and password in the textboxes below the "Enter 
 information to log on to your datbase" line.</p>
<p>Now, click on the "OK" button at the bottom of the screen. A line 
 of text should appear in the textbox under the "Use Connectionstring" 
 optionbox. Let's look at this line:</p>
<p> </p>
<p>Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual 
 Studio\VB98\NWIND.MDB;Persist Security Info=False</p>
<p> </p>
<p>Note the syntax. Provider=Microsoft.Jet.OLEDB.4.0; tells the PC to use Microsoft 
 Jet 4.0 OLE DB Database Provider. The ";" is used as a separator between 
 attributes. Data Source= tells the computer where the database is located. Persist 
 Security Info=False tells the computer you set no security attributes; that 
 is, the database is not passworded, nor does it do any kind of security check 
 before allowing editing, adding, removal, etc. of its data.</p>
<p>Now, you have to tell the PC to get its data from a certain table. Click on 
 the "RecordSource" tab. Select from the first drop-down box 2-adCmdTable, 
 and from the second drop-down box, select "Customers." Now you are 
 ready to create textboxes to display this data. At the end of this tutorial 
 you will be taught how to display the data in a datagrid. Click on the "OK" 
 button. Your program is now ready to connect to the database NWIND.mdb.</p>
<p>Next, we shall create three textboxes to hold the data of the ContactName, 
 Address, and City of the customers stored in the customer table of the NWIND.mdb 
 database. Create three textboxes, text1, text2, and text3 and align them like 
 this: Note, though, that your textboxes will not yet have any data in them. 
 That step comes later. </p>
<p align="center"><img src="http://campbellde.netfirms.com/t31.jpg" width="322" height="242"></p>
<p align="left">Click on text1 and goto the properties window (F4 to bring this 
 window to the front.) Find its DataSource property and set it to adoConnect. 
 Find its DataField property and set it to ContactName. Click on text2 and set 
 its DataSource property to adoConnect and its DataField property to Address. 
 Click on text3 and set its DataSource property to adoConnect and its DataField 
 property to City. Run your project (F5) and see if "Maria Anders" 
 appears in text1, "Obere Str. 57" appears in text2, and "Berlin" 
 appears in text3. If it has you have successfully written your first database 
 program. Try clicking on the ">" button on adoConnect to goto the 
 next record and watch the new names popup. </p>
<p align="left"> </p>
<p align="left">But don't you want to do more with your project? Don't you want 
 to edit, delete, and add records to your database? </p>
<p align="left"><i>Adding Records:</i></p>
<p align="left">We will first learn how to add records. To do this, we must first 
 finish the project. We must create a textbox for each field of the database. 
 Therefore, create eight more textboxes for the following fields:</p>
<ul>
 <li>ContactID</li>
 <li>CompanyName</li>
 <li>ContactTitle</li>
 <li>Postal Code</li>
 <li>Phone</li>
 <li>Region</li>
 <li>Country</li>
 <li>Fax</li>
</ul>
<p>Once you have those and they are carrying the correct information, add two 
 commandbuttons. These will complete the two step process of adding a record. 
 Set the caption of the first commandbutton to "Add" and the second's 
 caption to "Save." You see, to add a record, you must first make a 
 blank record, give the user time to input data, then save the edited record 
 into the table. </p>
<p>In the commandbutton "Add"'s _Click() event, put:</p>
<p> </p>
<p><font face="Courier New, Courier, mono">adoConnect.Recordset.Addnew</font></p>
<p> </p>
<p><font face="Times New Roman, Times, serif">In the commandbutton "Save"'s 
 _Click() event, put:</font></p>
<p> </p>
<p><font face="Courier New, Courier, mono">adoConnect.Recordset.Save</font></p>
<p> </p>
<p><font face="Times New Roman, Times, serif">Run the program and click on "Add." 
 All textboxes should go blank. Input information - any info you want and click 
 Save. All textboxes should return to the old data. Then, click on the "|>" 
 button on adoConnect. You should see the data you have just entered. You have 
 created a new record! </font></p>
<p> </p>
<p><i><font face="Times New Roman, Times, serif">Deleting Records: </font></i></p>
<p> </p>
<p>To delete records, you must create a commandbutton with the caption of "Remove." 
 In its Click() event, put:</p>
<p> </p>
<p><font face="Courier New, Courier, mono">adoConnect.Recordset.Delete</font></p>
<p> </p>
<p><font face="Times New Roman, Times, serif">Then, run the program and use the 
 "<" and ">" buttons to navigate to the record you 
 created in instructions on how to add records. DO NOT TRY TO DELETE ANY OTHER 
 RECORDS! If you do, it will give you an error that another table has "related 
 entries." This means that some of the data other tables are holding are 
 directly dependent on the data in the Customers table (for example if I have 
 Orders and Customers, I may say that John Doe has ordered a pair of pants. The 
 entry for a pair of pants in the Orders table is direclty dependent on the data 
 on John Doe that the Customers table holds.) The record that you created yourself 
 is 100% free of this hassle. Click on the "Remove" button. If nothing 
 happens, you are on the right track. Now, end the program and start it up again. 
 Your entry should now have been deleted. Congradulations. If you want to go 
 a step further refresh the database after you delete the record, simply put 
 this in the "Remove" button's Click() event under "adoConnect.RecordSet.Delete":</font></p>
<p> </p>
<p><font face="Courier New, Courier, mono">adoConnect.Refresh</font></p>
<p> </p>
<p><i><font face="Times New Roman, Times, serif">Editing Records:</font></i></p>
<p><font face="Times New Roman, Times, serif">Editing records is easy! Just navigate 
 to the record you wish to edit, change something, and click the "Save" 
 button. You're done!</font></p>
<p> </p>
<p><i><font face="Times New Roman, Times, serif">Viewing Data in a DataGrid:</font></i></p>
<p><font face="Times New Roman, Times, serif">This part of the tutorial is the 
 best part, for you can use your skills which you learned above to create a project 
 that makes the database look exactly like Excel! To begin, right click on the 
 toolbar and select Components.</font></p>
<p><font face="Times New Roman, Times, serif">In the components window, check 
 off Microsoft Chart Control and DataList Control. Click "OK." Add 
 the DataGrid control to your form. Set the DataGrid control's DataSource property 
 to adoConnect. Run the program and see all the data in a night neat grid. You 
 can now select, edit, delete, etc. records as you please!</font></p>
<p><font face="Times New Roman, Times, serif">Add a DataChart control to the form 
 and set its DataSource property to adoConnect. Unfortunately, though, the DataChart, 
 by nature, requires some sort of charable data. No table in NWIND.mdb has such 
 data. Therefore, I (being the author of this tutorial,) suggest that you create 
 your own database with a table called numbers with fields Name and Amount. This 
 is a perfect graphable value. Enter a bunch of names into the Name field like 
 Bob, Donald, Jim, John, Hal, etc. and random numbers in the Amounts field. Then, 
 rebuild the connection string (see above) to fit your database with your table. 
 Then, run your app and see the DataChart control display your data in a bar 
 chart. </font></p>
<p><font face="Times New Roman, Times, serif">To change the type of chart the 
 DataChart will display, right-click on it and click "Properties." 
 Select whether you want a 2D or a 3D graph and then select a type of graph from 
 the list. When done, click OK and run the program. See your new graph displayed 
 there.</font></p>
<p> </p>
<p><b><font face="Times New Roman, Times, serif">What to do next...</font></b></p>
<p><font face="Times New Roman, Times, serif">Next, try to read other advanced 
 tutorials on ado and try to create a querying system where you can search for 
 records. This action is a rather advanced action, and therefore I myself am 
 saving it for the next installment of this tutorial.</font></p>
<p><font face="Times New Roman, Times, serif">Thank you for reading. If you have 
 any comments or would like to tell me what you think, <a href="mailto:[email protected]">email 
 me here.</a></font></p>
<p> </p>
<p><a href="http://www.geocities.com/campbell_donalde/vb/dattut.zip">DOWNLOAD 
 THE SAMPLE APPLICATION</a></p>
<p> </p>
<p> </p>
<p> </p>
<p align="center"></p>
<p align="center"></p>
<blockquote>
<div align="left"></div>
</blockquote>
</body>
</html>
Bình luận gốc (3)
Được khôi phục từ Wayback Machine