> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# dbQuery

> 对数据库执行 SQL 查询，并逐行返回结果。

export const siteNameShort = "知行之桥";

对数据库执行查询。

## 必需的参数

* **driver**：跨平台版本中的 JDBC 驱动程序类名称，或 .NET 版本中的 ADO.NET 提供程序名称。
* **conn**：连接字符串或数据库 URL。
* **query**：SQL 查询字符串。

## 可选的参数

* **commandtimeout**：操作完成的 CommandTimeout（以秒为单位）。零 (0) 表示没有超时。默认值为 `60`。
* **fromrow**：从开头要跳过的行数。默认值为 `0`。
* **maxrows**：从数据库返回的最大行数。设置为 `-1`（默认值）可获取所有结果。
* **paramname#**：参数名称。
* **paramvalue#**：参数值。
* **paramtype#**：参数类型。
* **transactionid**：事务的 ID。
* **querypassthrough**：将查询按原样传递给操作，而不是执行客户端验证和语法纠正。

## 输出属性

* **db:\\**：输出因查询而异。

## 示例

此示例使用 {siteNameShort} 跨平台版本在 MySQL 数据库中执行查询。它从 film 表中 SELECT 评级为 PG-13 的所有电影，并为每部电影输出一个文件。文件名是电影标题，文件内容是电影描述。paramname/paramvalue 属性用于演示其在脚本中的工作方式。

```xml theme={null}
<arc:set attr="db.driver" value="cdata.jdbc.mysql.MySQLDriver" />
<arc:set attr="db.conn" value="jdbc:cdata:mysql:server=localhost;port=3306;database=sakila;user=root;password=Password123;"/>
<arc:set attr="db.query" value="SELECT * FROM `film` WHERE rating = @max_rating"/>
<arc:set attr="db.paramname#1" value="max_rating" />
<arc:set attr="db.paramvalue#1" value="PG-13" />

<arc:call op="dbQuery" in="db" out="results" >
  <!-- optional logging step to see the output of the query in the application log -->
  <arc:set attr="_log.info" value="[results.*]" />
  <!-- setting the data of each output file to be the description of the film -->
  <arc:set attr="output.data" value="[results.db:description]" />
  <!-- setting the output filename to be the title of each film -->
  <arc:set attr="output.filename" value="[results.db:title].txt" />
  <arc:push item="output" />
</arc:call>
```

只要在安装并运行 {siteNameShort} 的计算机上安装了驱动程序，就可以修改上面的示例以定位其他数据库。针对 .NET 调整此示例时，需要按如下方式更改 `driver` 和 `conn` 输入：

```xml theme={null}
<arc:set attr="db.driver" value="System.Data.CData.MySql" /> 
<arc:set attr="db.conn" value="Server=localhost;Database=sakila;UID=root;Password=Password123;"/>
```
