0

我试图从 Firebird SQL 2.5 数据库中转储数据,但几乎没有成功,因为我试图在表和存储过程之间建立连接。表示有问题的查询的代码如下:

select
    U.SYMBOL
    , '0' AS FIRM_ID
    , U.DATA_OD AS DATA
    , IIF(EXTRACT(DAY FROM U.DATA_OD) = 1 AND EXTRACT(MONTH FROM U.DATA_OD) = 1, 'NR', 'ZW') AS PRZYCZYNA_ZMIANY_WYM
    , PS.WYMIAR AS WYMIAR
    , PS.ZALEGLY AS WYM_ZAL
    , ' ' AS WYMIAR_INW
    , ' ' AS WYMIAR_INW_ZAL
    , ' ' AS NAZWA_ABSENCJI
from 
    P_PRA_WYM_URL U
    left JOIN PS_WYK_URLOPOW(U.SYMBOL, U.DATA_OD, U.DATA_OD) ps ON 1=1
where
    U.SYMBOL IN (SELECT SYMBOL FROM P_PRACOWNIK)

存储过程接受三个参数:PS_WYK_URLOPOW员工编号和两个日期。所有参数都是P_PRA_WYM_URL表的成员,因此存储过程需要为该表中的每条记录执行一次。该表中有超过 40 000 条记录。

我正在使用以下 C# 代码执行此查询:

if (AreSettingsPopulated())
            {
                string sFilepath = dSettings[FILEPATHKEY];
                string sUsername = dSettings[USERNAMEKEY];
                string sPassword = Encoding.UTF8.GetString(System.Convert.FromBase64String(dSettings[DBPASSWORDKEY]));

                string connectionString =
                                    "User={0};" +
                                    "Password={1};" +
                                    "Database={2};" +
                                    "DataSource=localhost;" +
                                    "Port=3050;" +
                                    "Dialect=1;" +
                                    "Charset=NONE;" +
                                    "Role=;" +
                                    "Connection lifetime=15;" +
                                    "Pooling=true;" +
                                    "MinPoolSize=0;" +
                                    "MaxPoolSize=50;" +
                                    "Packet Size=8192;" +
                                    "ServerType=0";

                FbConnection fbConn = null;

                try
                {
                    string sSqlQuery = "";

                    using (FileStream fs = new FileStream(string.Format("{0}.sql", dSettings[TABLENAMEKEY]), FileMode.Open))
                    {
                        using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                        {
                            if (!sr.EndOfStream)
                                sSqlQuery = sr.ReadToEnd();
                            sr.Close();
                        }
                        fs.Close();
                    }

                    if (sSqlQuery != "")
                    {
                        DataTable dtDumpResults = new DataTable(dSettings[TABLENAMEKEY]);

                        fbConn = new FbConnection(string.Format(connectionString, sUsername, sPassword, sFilepath));
                        FbCommand fbcDumpCommand = new FbCommand(sSqlQuery, fbConn);
                        fbcDumpCommand.CommandTimeout = 90000000;

                        if (fbConn.State != System.Data.ConnectionState.Open)
                            fbConn.Open();

                        using (FbDataAdapter fbdaDumpCommand = new FbDataAdapter(sSqlQuery, fbConn))
                        {
                            this.engine.WriteLine("Dumping the contents from database...");
                            fbdaDumpCommand.Fill(dtDumpResults);
                            this.engine.WriteLine("Dump complete!");
                        }

                        if (dtDumpResults.Rows.Count > 0)
                        {
                            GenerateExportXlsFile(dtDumpResults, dSettings[TABLENAMEKEY]);
                        }
                        else
                            throw new ArgumentNullException("The result set returned null!");
                    }
                    else
                        throw new ArgumentNullException("The SQL query is empty!");
                }
                catch (Exception ex)
                {
                    return string.Format("Error while invoking plugin: {0}", ex.ToString());
                }
                finally
                {
                    if (fbConn != null && fbConn.State == System.Data.ConnectionState.Open)
                        fbConn.Close();
                }
            }
            else
                return "Execution failed! The settings are not populated!";

            return string.Format("Export of table {0} complete!", dSettings[TABLENAMEKEY]);
        }

如果我尝试使用 Flamerobin 使用上面显示的查询转储数据,则预取机制会起作用并每 500 条记录加载一次数据,我可以获得完整的转储,而使用 C# 应用程序则执行停止fbdaDumpCommand.Fill(dtDumpResults);

问题是:如何优化查询以使其在 C# 应用程序中工作?

4

0 回答 0