Setting Up TSQLConnection 设置TSQLConnection.
TSQLConnection的中文资料太少了,这里是易博龙(embaradero)的英文资料,翻译成中文,水平有限,欢迎各位达人拍砖.
Setting Up TSQLConnection
设置TSQLConnection
原文地址:
http://docwiki.embarcadero.com/RADStudio/XE2/en/Setting_Up_TSQLConnection
Go Up to Using dbExpress Components Index
In order to describe a database connection in sufficient detail for TSQLConnection to open a connection, you must identify both the driver to use and a set of connection parameters the are passed to that driver.
为了更有效的描述数据库连接TSQLConnection(连接数据库的),你必须识别使用的数据库驱动,和数据库驱动的一系列参数。
Contents
- 1 Identifying the driver
- 2 Specifying connection parameters
- 3 Naming a connection description
- 4 Using the Connection Editor
- 5 See Also
内容
1.识别驱动程序
2.指定数据库连接参数
3.命名一个数据库连接描述
4.使用数据库连接编辑器
5.其他
Identifying the driver
The driver is identified by the DriverName
property, which is the name of an installed dbExpress driver, such as ASA, ASE, Datasnap, Db2, Firebird, Informix, InterBase, MSSQL, MSSQL9, MySQL, Odbc, and Oracle. These databases use Dynalink drivers, which are partially written in Object Pascal. For Dynalink drivers, the driver name is associated with these files:
- The dbExpress driver. This is a dynamic-link library with a name like dbx*.dll. For example, the Interbase driver DLL begins with "dbxint" and the Oracle driver DLL begins with "dbxora"
- The dynamic-link library provided by the database vendor for client-side support.
识别数据库驱动
属性DriverName用来区别数据库驱动,如下:dbExpress,ASA,ASE,Datasnap,DB2,Firebird,Informix,InterBase,MSSQL,MSSQL9,MySql,ODBC和oracle。这些数据库用动态链接库驱动,他们是Object Pascal的一部分。对动态链接库驱动来说,驱动库的名字和下面这些文件关联:
*dbExpress的数据库驱动。他是一个名字像dbx*.dll的动态链接库。例如:Interbase数据程序用dbxint开头,Oracle用dbxora
*动态链接库提供了数据库开发者对数据库应用程序的支持。
The relationship between these files and the database name is stored in a file called dbxdrivers.ini, which is updated when you install a dbExpressdriver. Typically, you do not need to worry about these files because the SQL connection component looks them up in dbxdrivers.ini when given the value of DriverName. When you set the DriverName property, TSQLConnection automatically sets the LibraryName and VendorLib properties to the names of the associated dlls. Once LibraryName and VendorLib have been set, your application does not need to rely on dbxdrivers.ini. (That is, you do not need to deploy dbxdrivers.ini with your application unless you set the DriverName property at run time.)
这些文件和数据库名字的关系被存储在文件dbxdrivers.ini中。 当你安装一个dbExpressdriver(数据库)时,这些文件会被更新。你不必担心这些文件,因为当SQL connection被设置了Drivername时,他们将查询文件dbxdrivers.ini,当你设置属性DriverName时, TSQLConnection自动设置属性LibraryName和VendorLib为相关联的dll。一旦LibraryName和VendorLib被设置了,你的应用程序将不依赖dbxdrivers.ini.(换言之,在运行期你的应用程序不需配置文件dbxdrivers.ini除非你重新设置了DriverName属性)
Specifying connection parameters
The Params property is a string list that lists name-value pairs. Each pair has the form Name=Value, where Name is the name of the parameter, andValue is the value you want to assign.
The particular parameters you need depend on the database server you are using. However, one particular parameter, Database, is required for all servers. Its value depends on the server you are using. For example, with InterBase, Database is the name of the .gdb file, with ORACLE it is the entry in TNSNames.ora, while with DB2, it is the client-side node name.
Other typical parameters include the User_Name (the name to use when logging in), Password (the password for User_Name), HostName (the machine name or IP address of where the server is located), and TransIsolation (the degree to which transactions you introduce are aware of changes made by other transactions). When you specify a driver name, the Params property is preloaded with all the parameters you need for that driver type, initialized to default values. You can also add other connection parameters to the Params string list, according to your application's needs. For example, to provide Multiple Active Result Set (MARS) support to a SQL connection using the MSSQL driver, you must add a key called Mars_Connection and set its value to True.
Because Params is a string list, at design time, you can double-click the Params property in the Object Inspector to edit the parameters using the String List editor. At run time, use the Params.Values property to assign values to individual parameters.
指定连接参数
属性Params是一个成对的name-value的字符串列表。你赋值的每对参数,名字是名字,值是值。
你使用的数据库服务有自己的特殊的参数。然而,一个特殊的参数,Database,对所有服务都是必须的。它的值依赖你在使用的数据库服务。例如:用数据库Interbase,Database的名字是文件.gdb,用数据库ORACLE,Database的全名是TNSNames.ora,用DB2时,它是客户端节点的名字(???)。
其他一个典型参数如UserName,Password,HostName(服务器名称或者服务器的地址),并且传输()时,你需要指定数据库的驱动名称,参数Params根据你需要的数据库驱动类型进行预装载,并被初始化为默认值。你能够根据应用程序增加Params列表的一些其他的连接参数。例如,提供并联的活动结果集(MARS)支持SQL连接,用MSSQL数据库驱动,你必须增加参数Mars_Connection,并设置它为True.
在设计期Params是个字符产列表,你可以在对象查看器中双击它,用字符串编辑器设置它的属性值。在运行期,可以通过赋值的方式设置Params属性值。
Naming a connection description
Although you can always specify a connection using only the DatabaseName and Params properties, it can be more convenient to name a specific combination and then just identify the connection by name. You can name dbExpress database and parameter combinations, which are then saved in a file called dbxconnections.ini. The name of each combination is called a connection name.
Once you have defined the connection name, you can identify a database connection by simply setting the ConnectionName property to a valid connection name. Setting ConnectionName automatically sets the DriverName and Params properties. Once ConnectionName is set, you can edit the Params property to create temporary differences from the saved set of parameter values, but changing the DriverName property clears both Params and ConnectionName.
One advantage of using connection names arises when you develop your application using one database (for example Local InterBase), but deploy it for use with another (such as ORACLE). In that case, DriverName and Params will likely differ on the system where you deploy your application from the values you use during development. You can switch between the two connection descriptions easily by using two versions of the dbxconnections.ini file. At design time, your application loads the DriverName and Params from the design-time version of dbxconnections.ini. Then, when you deploy your application, it loads these values from a separate version of dbxconnections.ini that uses the "real" database. However, for this to work, you must instruct your connection component to reload the DriverName and Params properties at run time. There are two ways to do this:
- Set the LoadParamsOnConnect property to True. This causes TSQLConnection to automatically set DriverName and Params to the values associated withConnectionName in dbxconnections.ini when the connection is opened.
- Call the LoadParamsFromIniFile method. This method sets DriverName and Params to the values associated with ConnectionName in dbxconnections.ini (or in another file that you specify). You might choose to use this method if you want to then override certain parameter values before opening the connection.
命名一个连接描述
尽管你可以通过属性DatabaseName和Params指定一个数据库连接,但是更有效的做法是通过绑定和辨别连接名称。你可以命名dbExpress数据库和参数绑定,他们被存在在文件dbxconnections.ini中。每一个连接名是一个组合名。
一旦你已经定义了连接名,你能够通过一些简单,有效的连接名字分辨一个数据库连接。通过ConnectionName,能自动设置属性DriverName和Params。一旦设置了连接名,你能够编辑属性Params去创建一个与保存的参数集不同的临时连接,但是一旦改变了DriverName将会清除Params和ConnecitonName。
用Conneciton names的一个优点是:当你开发一个数据库应用程序(例如:本地Interbase数据库),但是发布时是其他的数据库(比如:Oracle)。在那种情况下,DriverName和Params可能会与你开发时的值不同。你能够通过文件dbxconnections.ini轻易的转换两个数据库连接。在设计期,你的应用程序从dbxconnections.ini中装载DriverName和Params,然后,你发布应用程序时,程序能够从dbxconnections.ini装载实际的数据库。然而,为了程序能够正常运行,在运行期你必须通知你的连接组件去重新装载DriverName和Params。有以下两种方法:
*设置属性LoadParamsOnConnect为True。这是因为当连接数据库时TSQLConnection通过文件dbxconnections.ini中的ConnectionName自动设置DriverName和Params值。
*调用方法LoadParamsFromIniFile。这个方法dbxconnections.ini(或者通过你指定的文件)中的ConnectionName设置DriverName和Params值。在打开数据库连接之前,你可以通过这个方法覆盖(指定)特定的参数值。
Using the Connection Editor
The relationships between connection names and their associated driver and connection parameters is stored in the dbxconnections.ini file. You can create or modify these associations using the Connection Editor.
To display the Connection Editor, double-click the TSQLConnection component. The Connection Editor appears, with a drop-down list containing all available drivers, a list of connection names for the currently selected driver, and a table listing the connection parameters for the currently selected connection name.
You can use this dialog to indicate the connection to use by selecting a driver and connection name. Once you have chosen the configuration you want, click the Test Connection button to check that you have chosen a valid configuration.
In addition, you can use this dialog to edit the named connections in dbxconnections.ini:
- Edit the parameter values in the parameter table to change the currently selected named connection. When you exit the dialog by clicking OK, the new parameter values are saved to dbxconnections.ini.
- Click the Add Connection button to define a new named connection. A dialog appears where you specify the driver to use and the name of the new connection. Once the connection is named, edit the parameters to specify the connection you want and click the OK button to save the new connection to dbxconnections.ini.
- Click the Delete Connection button to delete the currently selected named connection from dbxconnections.ini.
- Click the Rename Connection button to change the name of the currently selected named connection. Note that any edits you have made to the parameters are saved with the new name when you click the OK button.
使用数据库连接编辑器
在Connection Names和他们相关联的在文件dbxconnections.ini中存储的Driver和connection parameters的关系中,你可以通过Connection Editor创建或者改变他们的关联关系。
通过双击组件TSQLConnection来现实Connection Editor。Connection Editor包含着当前可选的驱动的下拉列表,和当前可选Connection name的Connection parameter.
你可以使用这个对话框去,通过选择Driver和Connection name设置一个connection。 一旦你选择了你需要的配置,点击测试按钮来检查你的配置是否正确。
此外,你可以用这个对话框去编辑文件dbxconnections.ini中的named connections。
*改变当前你选择的named connection,通过编辑参数列表中的参数值。当你通过点击按钮OK时,你退出这个对话框时,新的参数值被保存在文件dbxconnections.ini中。
* 点击删除链接按钮,从文件dbxconnections.ini删除你当前选择的named connection。
* 点击重命名按钮,你可以对当前选择的named connection重新命名。记住,当你点击按钮OK时,你对Parameters所做的任何编辑都被保存成一个新的名字。