Delphi database programming skills

xiaoxiao2021-03-06  15

I was originally wandering around Wan Qian (News: //news.webking.com.cn/) Delphi version of the Delphi version, found that some people often put some database skills, the problem is not difficult, but to be one Simple solution, may also break thinking, especially here, simply talk:

1.

How to dynamically set an ODBC source

Many times the programmer needs to automatically generate an ODBC data source, not to guide the customer "to open the control panel ...", how to do it? I believe that many people choose programming to modify the registry to implement this feature, because the details of the ODBC are all stored in the following key values:

"HKEY_LOCAL_MACHINE / SOFTWARE / ODBC"

Modify the ODBC configuration, you can find a certain law through the front and rear registry. Here I just want to say, buddies, don't be so stupid (if I let you write a universal ODBC source handler, you have to die), use this Windows ODBC API function,

Function SqlconfigDataSource (HWndParent: Integer; FREQUEST: INTEGER;

LPSZDRIVERSTRING: STRING; LPSZATTRIBUTES: STRING): Integer;

STDCALL; External '' odbccp32.dll '';

Understanding database programming friends know that the database's access method is regardless of DAO, ADO, ODBC or BDE or other third-party database connection controls, all of which are some function sets, as long as you wish, you can write your own Database access methods are used in replacement. In-depth study these underlying functions, many times will provide you with quite convenient.

SqlconfigDataSource This function MSDN has a detailed description, I don't want the whole translation to let you flat, I just combined with the popular SQL Server talk about how to call this function. Other databases are similar.

SqlconfigDataSource (0, ODBC_ADD_SYS_DSN, '' SQL Server '', '' DSN = Record_oDBC '' CHR (0) '' Server = (Local) ' CHR (0) ' 'Database = Master' ' CHR (0) '' description = DragonPC SQLSERVER ODBC SOURCE '' CHR (0));

This is an instance of the function called this function in my Delphi program. The first parameter is the parent window handle, set to 0, the function does not display any dialog box. The second parameter is the type of operation, you need to define the following operation type constant:

Const ODBC_ADD_DSN = 1;. // Add a new user data source ODBC_CONFIG_DSN = 2; // Configure (modify) an existing user data source ODBC_REMOVE_DSN = 3;. // Remove an existing user data source ODBC_ADD_SYS_DSN = 4;. // Add . a new system data source ODBC_CONFIG_SYS_DSN = 5;. // Modify an existing system data source ODBC_REMOVE_SYS_DSN = 6; // Remove an existing system data source from the name we know, want to add an ODBC source, we need to call or is ODBC_ADD_SYS_DSN. ODBC_ADD_DSN parameters. The third parameter has nothing to say, we add an ODBC source of the SQL Server database, so fill in the '' SQL Server '' parameter, if you need to create an ODBC data source of an Excel file, we can fill in '' Excel Files (* .xls) '', these string parameters believe that all comrades have seen many times when adding ODBC sources.

The key is the setting of the third parameter. The keywords supported by different database types are different. Here is a brief description of the keywords supported by SQL Server:

DSN: Your ODBC Data Source Name.

Server: Your database server name, use (local) refers to a database installed by the local computer. Note: The latest SQL Server 2000 supports a computer running multiple SQL Server services, this time you need to specify the SQLSever's InstanceName.

Address: Specifies the network IP address of the SQL Server server.

Database: Specify the default database name.

Language: Specifies the default language.

Description: Remarks Information.

The detailed parameters and information can be found in the following URLs of the Microsoft website.

Http://msdn.microsoft.com/library/psdk/dasdk/odch3kit.htm

http://msdn.microsoft.com/library/psdk/sql/od_odbc_c_99yd.htm

2.

How to dynamically set BDE alias

This problem is actually the familiarity of the programmer's TSESSION components for BDE, the establishment of a database program, even if you don't have explicitly add Tsession components, there is still a TSession object that is a session in the system, you can call anywhere Method and attributes of this object. Many ways to help our app get a system BDE environment, let's introduce a code snippet to add a BDE alias:

VAR BDELIST: TSTRINGLIST ;. . . Begin. . . BDELIST: = TSTRINGLIST.CREATE (); try session.getaliasNames (bdelist); // Get all BDE alias list if BDELIST.INDEXOF ('' Dragonpc ') = -1 Then Begin // If there is no BDE alias Dragonpc "bdelist.clear; bdelist.add ('' server name = '' ')); // Database server name BDELIST.ADD (' 'Database Name = Master' '); // Default Database BDELIST .Add ('' user name = sa '); // User name session.addalias (' Dragonpc ',' 'MSSQL' ', BDELIST); // Add a MSSQL type BDE alias showMessage (' "system The BDE alias! '); Session.saveconfigfile (); // Storage BDE Configuration End; Finally Bdelist.Free; end; this simple, users can establish, delete and modify BDE alias (interested friends can view The source code of the TSESSION component is to see which BDE functions are called). In addition, deletealias, modifyalias, getdatabasenames, getdrivernames, getstoredprocnames, gettablenames, getpassword, etc. Tsession classes, very simple, through Delphi random help, readers can try to call themselves. Through the flexible application of the session, you can write a universal database query tool that is comparable to SQL Explorer.

3.

How to fade the error when running a SQL statement

Old, there is a friend to develop some open database interfaces (such as Delphi's SQL Explorer tool), since it is open, of course, it is necessary to allow users to use SQL statements to access the database, these also do, once the user runs the SQL statement error How do programmers caught this exception? Very simple, look at the following functions:

Const execSqlmode = 0; OpenSQLMODE = 1; Resultright = '' SQL Query Res Right ';. . . function RunSql (RunQuery: TQuery; Sqls: TStringList; var ErrorMsg: string; Mode: integer): integer; begin ErrorMsg: = ResultRight; Result: = 0; try RunQuery.DatabaseName: = '' RecordDB ''; RunQuery.SQL. CLEAR (); runquery.sql.addstrings (SQLS); if mode = execsqlmode damquery.execsql () else runquery.open (); Except on E: Exception do errormsg: = E.MESSAGE # 13 # 10 ' '--- The error is discovered ---'; end; end; friends understand, my function is simple, pass the SQL statement code segment as a parameter to the TQuery component, by setting the query mode (Execsqlmode, OpenSQLMODE to process data query statements (SELECT) returned by the result set or have no data operation language returned by the result set (Update, Delete, INSERT, CREATE, etc.). And the unusual causing, we fell all the anomalous old ancestors Exception, because all the exceptions in Delphi were inherited from Exception, such a simple SQL statement runs and exception handle, once returned ErrorMSG The value is not Resultright, and ErrorMSG will return an exception, and the programmer can determine to handle different situations.

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

New Post(0)