In Delphi, BDE is not set to connect to the database)

Source: Internet
Author: User
Tags mssql server

Three methods:

First, do not use the control to connect to the database through BDE, but for most Program Not practical,
The second is to call the bde api function to complete alias modification. This method is feasible, but we need to learn the complex bde api functions, in addition, some encapsulation functions of BDE are in conflict with standard functions of Delphi. This method is more troublesome than the third method mentioned in this article.
The third method is to set the Params attribute of the database component in the program to replace the configured BDE alias function. The following describes the third method in detail:
In fact, all the alias attributes of BDE have been encapsulated in the Params attribute in the tdatabase control of Delphi. By modifying this attribute, your application can adapt to different users. It can even be easily converted between the standalone version and the online version. The following describes how to set local database connection and MSSQL database connection. For other types of databases, such as DB2 or other types, as long as you set BDE, you can bypass the class.
Set local database:
A local database is a database data file of paradox and a DBF data file of FoxPro. A database is a folder that stores these data files. You only need to set the path attribute in BDE. Here you only need to set this main Params. See the following Code :
VaR Database: tdababase;
Query: tquery;
Spath: string;
Function opendatabase: Boolean;
Begin
Getdatabasepath; // obtain the database storage path, which can be read from the registry or selected by the user or in any other way.
If directoryexists (Spath) then
Begin
Database. loginprompt: = false;
Database. databasename: = 'mydatabas'; // customize a databasename;
Database. aliasname: = '; clear the aliasname attribute;
Database. drivername: = 'standard'; // set the local database to standard;
Database. Params. Clear;
Database. Params. Add ('path = '+ Spath // you can specify the database path;
Query. databasename: = database. databasename;
Query. SQL. Clear;
Query. SQL. Add ('...'
Try
Query. open;
Except
Showmessage ('Open table error! '
Raise;
End;
End;
End;
Set MSSQL database:
Setting the MSSQL database is a little more complex than the local database. You need to set more content. In BDE, you need to set the server name (the name of the machine connected to the MSSQL Server), Database Name (connected database name), user name (Database User Name, available Super User Name SA for the newly installed MSSQL), you also need to enter: Password (Database User Password, the newly installed MSSQL superscript user password is blank ). You only need to set these items in the program: Please refer to the Code:
VaR Database: tdababase;
Query: tquery;
Sservername, sdatabasename, susername, spassword: string;
Function opendatabase: Boolean;
Begin
Getdatabaseparams; // obtain the four parameters required for database connection, which can be read from the registry or selected by the user or in any other way.
Database. loginprompt: = false;
Database. databasename: = 'mydatabas'; // customize a databasename;
Database. aliasname: = '; clear the aliasname attribute;
Database. drivername: = 'mssql'; // set MSSQL to MSSQL;
Database. Params. Clear;
Database. Params. Add ('server name = '+ sservername // you can specify the database path;
Database. Params. Add ('database name = '+ sdatabasename // you can specify the database path;
Database. Params. Add ('user name = '+ susername // you can specify the database path;
Database. Params. Add ('password = '+ spassword // you can specify the database path;
Try
Database. Open; // try to open the database.
Except
Showmessage ('Open Database Error! '// If the database cannot be opened.
Raise;
End;
Query. databasename: = database. databasename;
Query. SQL. Clear;
Query. SQL. Add ('...'
Query. open;
End;
The program can be input by the user at the first startup, and then recorded in the registry or elsewhere. For the second time, you do not need to input the program and read it directly from the last record. If the server modifies the program, you can modify the record, or simply clear the records after opening the database, and then input them by the user.
Careful readers may notice that the local database determines whether the data can be opened by opening the data of a table because the database can be opened even if the folder does not exist. The MSSQL database is different. If an error occurs when the database is opened, the following situations may occur: the network connection is abnormal, the MSSQL Server is not started, or the database, user name, or password that is not specified by the MSSQL Server is incorrect.

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.