检索&更新20000条数据记录停止工作

发布于 2024-09-25 06:15:46 字数 1797 浏览 3 评论 0原文

我使用以下代码从 MS SQL 数据库获取所有数据记录,并尝试更新每条记录。该代码在 WebService 中使用。问题是,如果我有 1000 条数据记录,代码运行良好,但现在我有 20000 条数据记录,并且代码首先返回超时。然后我将 cmd.CommandTimeout 设置为零以没有超时。现在,当我在 IE WebSvc 中调用该函数时,IE 窗口仍然是空白的,并且仍然尝试加载某些内容,但没有任何反应。仅更新了 150 条数据记录。

您知道问题可能出在哪里吗?代码不是最好的,那么我应该改变什么?

非常感谢! WorldSignia

我的代码:

 private string AddNewOrgBez()
        {
            try
            {
                SqlConnection sqlconn = new SqlConnection(this.connectionString);
                SqlCommand cmd;
                SqlDataReader reader;
                sqlconn.Open();

                cmd = new SqlCommand("SELECT * FROM dbo.mydata", sqlconn);
                cmd.CommandTimeout = 0;
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    // Felder holen
                    string okuerzel = reader["O_KURZ"].ToString();
                    string bezeichnung = reader["O_BEZ"].ToString();

                    string[] lines = CreateNewOrgBez(bezeichnung);

                    string sqlcmd = "UPDATE dbo.mydata SET WEB_OBEZ1 = '" + lines[0] + "', WEB_OBEZ2 = '" + lines[1] + "', WEB_OBEZ3 = '" + lines[2] + "' WHERE O_KURZ = '" + okuerzel + "'";

                    SqlConnection sqlconn2 = new SqlConnection(this.connectionString);
                    sqlconn2.Open();
                    SqlCommand cmd2 = new SqlCommand(sqlcmd, sqlconn2);
                    cmd2.CommandTimeout = 0;
                    cmd2.ExecuteNonQuery();
                    sqlconn2.Close();
                }

                reader.Close();
                sqlconn.Close();

                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

I am using the following code do get all data records from a MS SQL database and I try to update every single record. The code is used in a WebService. The issue is, that the code runs fine if I have 1000 data records but now I have 20000 data records an the code first returned with an timeout. Then I set the cmd.CommandTimeout to zero to have no timeout. Now when I invoke the function in the IE WebSvc the IE window is still blank and still try to load something but nothing happens. Only 150 datarecords are updated.

Do you have any idea where the issue might be ? Is the code not the best, so what should I change ?

Thank you very much!
WorldSignia

MyCode:

 private string AddNewOrgBez()
        {
            try
            {
                SqlConnection sqlconn = new SqlConnection(this.connectionString);
                SqlCommand cmd;
                SqlDataReader reader;
                sqlconn.Open();

                cmd = new SqlCommand("SELECT * FROM dbo.mydata", sqlconn);
                cmd.CommandTimeout = 0;
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    // Felder holen
                    string okuerzel = reader["O_KURZ"].ToString();
                    string bezeichnung = reader["O_BEZ"].ToString();

                    string[] lines = CreateNewOrgBez(bezeichnung);

                    string sqlcmd = "UPDATE dbo.mydata SET WEB_OBEZ1 = '" + lines[0] + "', WEB_OBEZ2 = '" + lines[1] + "', WEB_OBEZ3 = '" + lines[2] + "' WHERE O_KURZ = '" + okuerzel + "'";

                    SqlConnection sqlconn2 = new SqlConnection(this.connectionString);
                    sqlconn2.Open();
                    SqlCommand cmd2 = new SqlCommand(sqlcmd, sqlconn2);
                    cmd2.CommandTimeout = 0;
                    cmd2.ExecuteNonQuery();
                    sqlconn2.Close();
                }

                reader.Close();
                sqlconn.Close();

                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半仙 2024-10-02 06:15:46

您在这里泄漏了每个 SqlCommand - 我建议您检查对 SqlClient 类的使用,以找到 IDisposable 的类并重新构造您的代码以确保使用 使用 构建。

例如,即使括号内的代码中存在异常,这也可以确保调用 Dispose

using (SqlCommand cmd2 = new SqlCommand(sqlcmd, sqlconn2))
{
  cmd2.CommandTimeout = 0;
  cmd2.ExecuteNonQuery();
}

为每个 UPDATE 使用新的 SqlConnection 也很昂贵,这应该在环形。冗余连接建立可能是超时的原因。

请注意@ck 的评论,即就效率而言,这种零碎的客户端操作不如在服务器端执行繁重的操作。您应该能够使此代码更好地工作,但这并不意味着它是理想/最快的解决方案。

You are leaking every SqlCommand here - I suggest you review your use of SqlClient classes to find the ones that are IDisposable and restructure your code to ensure they are always freed, using the using construct.

For example, this ensures Dispose gets called even if there is an exception in the bracketed code:

using (SqlCommand cmd2 = new SqlCommand(sqlcmd, sqlconn2))
{
  cmd2.CommandTimeout = 0;
  cmd2.ExecuteNonQuery();
}

Using a new SqlConnection for every UPDATE is expensive too, this should be done outside the loop. Redundant connection establishment is likely the explanation for your timeout.

Take note of @ck's comment that for efficiency this type of piecemeal client-side operation is not as good as doing the heavy lifting server-side. You should be able to get this code working better, but that does not mean it's the ideal/fastest solution.

oО清风挽发oО 2024-10-02 06:15:46

我发现了这个问题。
您首先需要获取所有数据记录,例如在新的 DataTable 中。我使用的结构不起作用,因为它从数据库读取数据并更新数据库。将其更改为新结构后,它就可以工作了。

I found the issue.
You need first to get all the data records, for example in a new DataTable. The structure I used does not work, because it reads data from the database and also updates the database. After changing it to a new structure it works.

霊感 2024-10-02 06:15:46

您使用两个不同的连接来读取和更新,其中一个连接阻塞了另一个连接。这就是为什么当您首先读取所有数据时,它就开始起作用。

You were using two different connections to read and to update, and one of them was blocking another. This is why when you read all your data first, it began to work.

半衬遮猫 2024-10-02 06:15:46

我怀疑您是否遇到了 OutOfMemoryException。您可以分析您的应用程序并检查内存使用情况吗?

既然您只是覆盖 While 循环中的变量,为什么不尝试将它们从循环中取出。

I doubt if your running into OutOfMemoryException. Can you profile your application and check the memory usage?

Since you are just overwriting the variables in While loop, why don't you try taking them out of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文