0

我有一个网站,不时需要我对 Access 数据库进行批量更新,为此我创建了一个运行一次并删除的页面,其中包含一个大文本框、一个验证码和一个硬编码的强密码。当我对数据库的本地副本运行它时,我没有问题,但是当我在服务器上运行它时,我在单击提交后大约 3 秒得到“与服务器重置的连接”。不幸的是,托管环境不在我的权限范围内,所以当这种情况发生时,我无法查看任何服务器端日志。我一点也不知道是什么原因造成的,所以我希望你们能看看。

这段代码有点难看,因为我真的不想花很多时间在上面(其中一些是由另一个开发人员编写的)。无论哪种方式,我都没有看到任何明显的功能问题。

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 回答