0

Access データベースで一括更新を行う必要がある Web サイトがあり、これを容易にするために、大きなテキスト ボックス、キャプチャ、およびハードコードされた強力なパスワードを含む一度だけ実行して削除するページを作成しました。これをデータベースのローカル コピーに対して実行すると問題はありませんが、サーバー上で実行すると、送信をクリックしてから約 3 秒後に「Connection with Server Reset」が表示されます。残念ながら、ホスティング環境は私の範囲外であるため、これが発生したときにサーバー側のログを確認することはできません. 何が原因なのかさっぱりわからないので、見ていただけると幸いです。

このコードは、私があまり時間をかけたくないという理由だけで少し醜いです (また、一部は別の開発者によって書かれています)。いずれにせよ、明らかな機能上の問題は見られません。

protected void btnRunBatch_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        ArrayList queries = GetSqlStatementArray();
        string results = string.Empty;

        try
        {
            BatchSQLInsert(Application["DBPath"].ToString(), queries);

            if (queries.Count > 0)
            {
                results = "<b>Batch Operation Completed Successfully.</b><br /><br />";
                results += "The following queries were executed:";
                results += "<ul>";
                foreach (string query in queries)
                {
                    results += "<li>" + query + "</li>";
                }
                results += "</ul>";

                this.tbxBatchStatement.Text = string.Empty;
            }
            else
            {
                results = "<b>No queries to execute.</b>";
            }
        }
        catch (Exception ex)
        {
            results = "<b>Execution Errors Encountered:</b><br />";
            results += "<ul><li>" + ex.Message + "</li></ul>";
        }

        this.phResults.Controls.Add(new LiteralControl(results));
    }
}

private ArrayList GetSqlStatementArray()
{
    ArrayList queries = new ArrayList();
    string[] lines = this.tbxBatchStatement.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    string lineBuffer = string.Empty;

    foreach (string line in lines)
    {
        if (lineBuffer == string.Empty)
        {
            if ((line.ToUpper().StartsWith("SELECT") || line.ToUpper().StartsWith("INSERT") || line.ToUpper().StartsWith("UPDATE") || line.ToUpper().StartsWith("DELETE")))
            {
                if (line.EndsWith(";"))
                    queries.Add(line);
                else
                    lineBuffer = line;
            }
        }
        else
        {
            lineBuffer += " " + line;
            if (line.EndsWith(";"))
            {
                queries.Add(lineBuffer);
                lineBuffer = string.Empty;
            }
        }
    }
    return queries;
}

public static void BatchSQLInsert(string DBPath, System.Collections.ArrayList sqlArray)
{
    System.Data.OleDb.OleDbCommand cmd = null;
    System.Data.OleDb.OleDbConnection conn = null;
    System.Data.OleDb.OleDbTransaction myTrans = null;
    int intRecsReturned = 0;
    int i = 0;

    if (sqlArray.Count > 0)
    {
        cmd = new System.Data.OleDb.OleDbCommand();
        conn = GetConnection(DBPath);
        myTrans = conn.BeginTransaction();

        try
        {
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = conn;
            cmd.Transaction = myTrans;
            while (i < sqlArray.Count)
            {
                cmd.CommandText = (string)(sqlArray[i]);
                intRecsReturned = cmd.ExecuteNonQuery();
                i++;
            }
            myTrans.Commit();
            conn.Close();
        }
        catch (Exception eee)
        {
            myTrans.Rollback();

            throw new Exception("BatchSQLInsert() failed-" + eee.Message + "\r\nArray-" + sqlArray.ToString());
        }
        finally
        {
            if (conn != null)
                conn.Dispose();
            if (cmd != null)
                cmd.Dispose();
        }
    }
}
4

1 に答える 1

1

プロセスの実行時間が長すぎるようです。IISのタイムアウト設定を調整する必要があるかもしれません...:(

私はあなたができないことを知っています、申し訳ありませんが本当に役に立ちません-

于 2009-08-18T21:44:51.583 に答える