Delphi programming access to SQL Server database

Source: Internet
Author: User
Tags odbc

Delphi is an excellent visual software development environment, and has been widely used in the development of database software. In the DELPHIC/S version environment, combined with its optimized database operation, and Borland db Engine (i.e.: BDE, Database Engine), it provides a faster and more efficient way to develop the network database software under the client/server system. In the client/server system, we use Microsoft SQL Server6 on the server side. 0 for WindowsNT as a database server, in addition, Delphi in the client to write client software. At this point, we assume that SQL Server Setup is complete and has been started, which only describes the client's program-writing steps. From the following article, the reader can find that you do not need to write a program, you can implement the operation of the SQL Server database. The specific steps are as follows:

The first step is to register an ODBC data source. This is a critical step, otherwise access to the database cannot be achieved. For example, we define a data source ZHB that can access a ZHB database on a SQL Server server. First, select the SQL Server type database and go to the ODBC SQL Server Setup window. Then, define the data source name as "ZHB", define the Server as "Sql-server", and the network path to "Ιιsql-server", and then press the option command button to select the database to be accessed as ZHB.

The second step is to configure BDE. It is a database engine dedicated to Delphi. It can be started from Delphi program group or from the Tools menu of Delphi Program. Delphi is slightly different from VB when accessing SQL Server databases. VB is directly called ODBC to connect to SQL Server, and Delphi is the first call to the alias in Bde, alias and then through the BDE ODBC driver direct access to the ODBC data source implementation. In BDE ODBC driver you can also define other ODBC properties, such as language drivers or open modes, and so on. This has undoubtedly greatly enhanced the capabilities of ODBC.

First, in the Drivers markup page, press "New ODBC Driver" to add a newer driver. There are three columns in the Add window that need to be filled in. The first column, SQL Link driver, can take one, such as: ODBC-ZHB, select SQL Server in the second column of default ODBC driver, and select the data source you just registered in the third column, default, ZHB. Press OK to confirm the addition of a DELPHIODBC driver named ODBC-ZHB. In the parameter table on its right, you can also set open mode OpenMode, query mode sqlqrymode, SQL statement execution mode SQL? Passthrumode and language driver Langdriver. Next, in the Aliases markup page, press the "newalias" command button to add a new alias. There are two columns to fill in the Add New alias window. The first column is Newaliasname, which can be used as a name, such as: ZHBDB; This alias is the name of the database that we want to call directly in the table control. The second column Aliastype select the C-ZHB that you just defined in the drivers markup page, and finally press OK to add an alias named Zhbdb. In this way, the table control can be connected to the ODBC-ZHB database on the ZHB server via the alias Zhbdb, alias Zhbdb through Delphi's Odbcdrivers (that is, ODBC-ZHB), Sql-server and then through the ODBC data source ZHB. This is equivalent to completing the registration in BDE, and then you can set the properties of the control.

The third step is to set the data access control properties to implement a connection to the database. Add table and DataSource controls to the form and modify the properties of the table control. First, change the DatabaseName property to the zhbdb that you just defined in the BDE alias. After you set the DatabaseName property, you can select the name of a table from the Drop-down list of the TableName property. In the meantime, the table control will complete a connection to the database, and after the connection is successful, the list of optional tables appears. Then, change the active property to True to create a connection to the open database activation and the DataSource control. Change the exclusive property to True. Finally, set the DataSet property of the DataSource control to Table1. The datasource control is the channel in which the table control is associated with the Datacontrols control. The DataSource1 object is associated with a table in the ZHB database by setting up the steps above. Later, the operation of the table can be implemented as long as the DataSource1 object is accessed.

Step fourth, set the data controls control properties to implement the operation of the database. Once a control for the data access class is opened successfully, the control of the Data controls tag page in the window is available. These controls are DBGrid, Dbnavigator, Dbtext, Dbedit, Dbmemo, Dbimage, Dblistbox, Dbcombobox, Dbcheckbox, and Dbradiogroup. These controls enable read-write operations on a table by setting their DataSource and datafields properties.

It can be seen that Delphi in the design of database software, especially SQL Server database is faster than VB, and its rich control for the interface design provides a beneficial help; In addition, Delphi's true compilation makes the program execution efficiency is quite high, the portability is good. These are the lack of VB.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.