gpt4 book ai didi

c# - 输出多个查询

转载 作者:行者123 更新时间:2023-11-30 22:00:41 26 4
gpt4 key购买 nike

我正在执行 oracle 查询并将所述查询的结果写入 txt 文件。

现在,如果正在处理自定义查询(由 bool 值 isCustom 设置),我需要更改架构

模式正在改变,查询正在执行,但由于某种原因,查询输出没有写入输出文件。

如果 isCustom 设置为 false,一切正常。

如有任何建议/提示,我将不胜感激。

public void OracleConnect(string queryString, string fileName, bool isCustom = false)
{
//downloaded oracle managed data access.dll
//changed reference
//changed connection to long unfunny one
//http://stackoverflow.com/questions/659341/the-provider-is-not-compatible-with-the-version-of-oracle-client#25412992
log.Debug("Task " + Task.CurrentId + ": executing " + queryString);
string oradb = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + host + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" + instance + ")));User Id = " + oracleUser + "; Password =" + oraclePassword;
try
{
//create connection
OracleConnection conn = new OracleConnection(oradb);
conn.Open();

OracleCommand cmd = new OracleCommand();
//if a custom query is being handled change the schema
if (isCustom == true)
{
log.Debug("##Test executing custom query!");
string changeSessionString = "ALTER SESSION SET CURRENT_SCHEMA = " + resultUser;
cmd.CommandText =
"begin " +
" execute immediate '"+ changeSessionString +"';" +
" execute immediate '"+ queryString+"';" +
"end;";
log.Debug("##Test custom query command = " + cmd.CommandText);
}
else
{
queryString = queryString.Replace("%pumaUser%", resultUser);
cmd.CommandText = queryString;
}

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;

OracleDataReader reader = cmd.ExecuteReader();

//get and write content to file
List<string> columnName = new List<string>();
columnName = GetColumnNames(reader);
using (StreamWriter writer = new StreamWriter(fileName))
{
//write query command to file
writer.WriteLine(queryString + Environment.NewLine);

//write header / column names into first line
StringBuilder header = new StringBuilder();
for (int i = 0; i < columnName.Count; i++)
{
header.Append(columnName[i].ToString());

if (i != columnName.Count - 1)
header.Append("\t");

}
writer.WriteLine(header);

//fill rest of lines with data
while (reader.Read())
{
StringBuilder row = new StringBuilder();
for (int i = 0; i < reader.FieldCount; i++)
{
row.Append(reader[i]);

if (i != reader.FieldCount - 1)
row.Append("\t");
}
writer.WriteLine(row);
}

}
log.DebugFormat("Task: {0} Writing query result into file: {1}", Task.CurrentId, fileName);

conn.Dispose();
}
catch (OracleException e)
{
log.ErrorFormat("Task {0} had an OracleError: {1}", Task.CurrentId, e.Message);
Helpers.writeErrorFile(fileName, e.Message);
}
catch (Exception e)
{
log.ErrorFormat("Task {0} Could not connect to database: {1} ", Task.CurrentId, e.Message);
}
}

最佳答案

> -for pl/sql procedures:   set serveroutput on

将输出写入文本文件

>     1.spool <location\xxx.txt>   after finishing 
> 2.spool off

关于c# - 输出多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com