Delphi Connection database for use

Source: Internet
Author: User

DELPHI Database Development There are many kinds of, can connect Access database, MS SQL Server database, Oracle database, MySQL database and so on, general connection in two ways: BDE and ADO Two ways, are to establish a connection, Using commands to execute SQL statements (queries, insertions, deletions, updates, and so on), the core part of the database operations in the background is SQL statements, which are similar to the operations on different platforms. The basic operation of connecting to a database is illustrated in the example of connecting MS SQL Server.



Delphi Connect MS SQL Server with ODBC, first set up the ODBC data source connection parameters.

1. Create a database

Open Start/program/microsoft SQL server/Enterprise Manager, expand the directory for Console root, right-click on the directory item labeled "Database", and then click "New Database", such as "DB1". Then right click on the table, create a new table, such as "T_user", and insert some data.



2. Setting up an ODBC data source

Open Start/Settings/Control Panel/Administrative Tools/Data source (ODBC), under "User DSN", click Add, select "SQL Server", complete, enter the data source name, such as "Deltest", fill in the server instance name:

Follow the next step, go to the last page, and then click Test Data Source. Detects if the settings are correct. If "Tests completed successfully!" is displayed correctly The information.



3. Set the data engine  

Open Start/program/borland Delphi7/bde Administrator, under the Databases node, you can see the ODBC data source that you just set up Deltest ", Right click, select" OPEN ", the input" user name "," Password "dialog box, enter the user name administrator and password and click" Confirm "to make it effective. At this point, if the connection is normal, the font for the project name on the right becomes bold. Note: To modify the parameter values, you should right click on the left side of the database name, select "Close", after modifying the parameter values, right-click on the left side of the database name, select "Apply" Save, and then select "OPen" to make the new parameter values take effect.  

4. Establish a connection  

Add the following components to the Delphi form: Database, Query, Datasource, DBGrid. Where database is the bridge connecting the SQL Server databases and Tquery controls, Tquery completes the specific query requirements, DBGrid is added to display the contents of the database. &NBSP

1), set database  first,

double-click the database component, the Database Parameter Settings dialog box appears, select or enter the BDE in alias name The name of the new database in Administrator, in this case deltest, enter a new name for it in name, such as "Dtest", and then click the "Defaults" button, if you do not want to enter the user name and password frequently, You can write a password in the parameter entry password and determine that the login Paromp entry is not selected. In the connected property, set to "true" to connect SQL server. 



2), set dbgrid ,

Set the DataSource property of the Dbgrid1 control, connect to Datasource1, and then set the DataSet property of the Datasource1 control. Connect to Query1. &NBSP

3), set query 

in the Query1.databaename property, from the drop-down list, select just enter the new name "Dtest" in the database setting, and in the SQL property, enter " SELECT * from T_user ", then click on the active property to" true " 

now look at DBGrid display, the data in the SQL Server database has been passed, you can do database development.  

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.