Use Delphi programming access SQLServer database

xiaoxiao2021-03-06  38

Use Delphi to program access to SQLServer database ---------------------------------------------------------------------------------------------------------------------------------- ------------------------------------- 2003-6-28 Source: Delphi Technology Network

Delphi is an excellent visual software development environment and has been widely used in database software development. In the Delphic / S version, it combines its optimized database operation, and Borland Database Engine (ie: BDE, Database Engine), provides a more fast and efficient way to develop client / server systems. In the client / server system, the server side we use Microsoft SQL Server6.0 for Windows to serve as the database server; in addition, the client software is written in Delphi. Here, we assume that the SQL Server setting is complete, and it has been launched, and only the client programming step is introduced. From the following article, the reader can find that you don't need to write a program, you can implement the operation of the SQL Server database. Specific steps are as follows:

The first step is to register the ODBC data source. This is a crucial step, otherwise it is impossible to implement the database access. For example, we define a data source zhb that enables the ZHB database on the SQL Server server. First, select the database of SQL Server type to enter the "ODBC SQL Server Setup" window. Then, define the data source name "zhb"; define the server as "SQL-Server"; and the network path is "ιιSQL-Server"; finally, press the Option command button to select the database to zHB.

Step 2, configure BDE. It is a Delphi dedicated database engine. You can start from the Delphi program group or start from the Tools menu of the Delphi program. Delphi is slightly different from VB when accessing the SQL Server database. VB is directly calling ODBC to connect SQLServer, and Delphi is first calling an alias in BDE, and the alias is reached directly through the ODBC Driver in BDE to directly access the ODBC data source. In the ODBC Driver in BDE, you can also define other ODBC properties, such as language drivers or open modes, and more. This undoubtedly greatly enhances the function of ODBC.

First, in the DRIVERS tag page, press "New ODBC Driver" to add a new driver. There are three columns to be filled in in the addition window. The first column SQL Link Driver can take one, such as: odbc-zhb; selection SQL Server in the second column DEFAULT ODBC DRIVER; select the data source ZHB just registered in the third column DEFAULT DATA SOURCE NAME. A DelphiodBC driver called ODBC-ZHB was added after the OK was confirmed. In the parameter table on the right side, you can also set the open mode OpenMode, query mode SQLQRYMODE, SQL statement execution mode SQL -PASSTHRUMODE, and language driver LangDriver et al. Next, in the AliaSes tag page, press the "Newalias" command button to add a new alias. There are two columns that need to be added in the new alias window. The first column is NewaliasName, you can take a name, such as: zhbdb; this alias is the database name we want to call directly in the Table control. The second column AliaSType select the odbc-zhb that just defined in the Drivers tag page, and finally press OK to confirm, add an alias called ZHBDB. In this way, the Table control can be connected to the zhb database connected to the SQL-Server server through the ODBC Data Source ZHB through the odbcdrivers (ie: odbc-zhb), ODBC-ZHB, and ODBC-ZHB, and ODBC-ZHB. The above is equivalent to completing registration in BDE, and then the property of the control can be set. In the third step, set the Data Access control property to implement the connection with 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 defined in the BDE alias. After setting the DatabaseName property, you can select a table name from the drop-down list of the TABLENAME attribute. This Table control is to complete the connection with the database, and an optional table name will appear in this list after the connection is successful. Then, change the Active property to TRUE, activate the open database, and connect to 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 that Table control is associated with the DataControls control. Through the settings of the above steps, the DataSource1 object has established contact with a table of the zhb database. In the future, you can implement the operation of the table as long as DataSource1 object is accessible.

Step 4, set the Data Controls control properties to implement the operation of the database. Once the control of the Data Access class is successfully opened, the control of the Data Controls tag page in the window can be used. These controls have DBGRID, DBNAVIGATOR, DBTEXT, DBEDIT, DBMEMO, DBIMAGE, DBLISTBOX, DBCOMBOX, DBCHECKBOX, and DBRADIOGROUP. These controls can implement the read and write operations of the table as long as they set their DataSource and DataFields properties.

It can be seen that Delphi is more fast than VB in designing database software, especially SQL Server databases; and its rich control provides favorable help to interface design; in addition, Delphi's true compilation makes the program's execution efficiency is quite high. Good portability. These are owed by VB.

转载请注明原文地址:https://www.9cbs.com/read-118348.html

New Post(0)