开发者

Why does my application hang while updating database records?

开发者 https://www.devze.com 2023-04-01 00:07 出处:网络
The following application code hangs while updating database records. It only displays 0 in output screen, and program hangs forever.

The following application code hangs while updating database records. It only displays 0 in output screen, and program hangs forever. However there are more than 50,000 records in table. (The program converts ISCII codes to UNICODE and writes them back into the database.)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
using System.Data.Sql;

namespace demoupdateform
{
public partial class Form1 : Form
{
    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test1.txt");

    String[,] harltabcol = new String[,]
        {
            {"Col_name","Table_name"}

        };

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection("Password=admin;Persist Security Info=True;User ID=sa;Initial Catalog=dbname;Data Source=DEEPAK-87E8B4");
        SqlConnection cn1 = new SqlConnection("Password=admin;Persist Security Info=True;User ID=sa;Initial Catalog=dbname;Data Source=DEEPAK-87E8B4");



        string temp;
        string temp1;
        try
        {

            for (int i = 0; i < harltabcol.GetLength(0); i++)
            {

                    cn.Open();


                SqlCommand cmdSel = new SqlCommand( "select [" + harltabcol[i, 0] + "] from [" + harltabcol[i, 1] + "]",cn);
                //cn.Open();
                SqlDataReader rdr = cmdSel.ExecuteReader();
                progressBar1.Value = 0;
                int j = 0;
                while (rdr.Read())
                {

                    temp = (rdr[harltabcol[i, 0]].ToString()).ToString();

                    temp1 = (Iscii2Unicode(temp)).ToString();
                    SqlCommand cmdUpd = new SqlCommand("UPDATE [" + harltabcol[i,1] + "] SET [" + harltabcol[i,0] + "] =N'"+temp1+"' WHERE " + harltabcol[i,0] + "='" + temp + "'",cn1);
                    temp = rdr[harltabcol[i, 0]].ToString();
                    cn1.Open();
                    cmdUpd.CommandTimeout = 0;
                    Console.WriteLine(j++);
                    file.WriteLine(temp+" --> "+temp1);
                    progressBar1.Value = progressBar1.Value + 1;

                    if (progressBar1.Value == 99)
                        progressBar1.Value = 0;

                    cmdUpd.ExecuteNonQuery();
                    temp = null;
          开发者_StackOverflow中文版          temp1 = null;
                    cn1.Close();
                }
                progressBar1.Value = 100;
                cn.Close(); 
            }


        }
        catch (Exception ex)
        {

            MessageBox.Show("Error Occured " + ex.ToString());
        }


    }
    public string Iscii2Unicode(string IsciiStr)
    {
        Encoding EncFrom = Encoding.GetEncoding(1252);
        Encoding EncTo = Encoding.GetEncoding(57002);
        Byte[] b = EncFrom.GetBytes(IsciiStr);

        return EncTo.GetString(b);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}
}


Normal. You don't give the message loop a chance to run. Use a BackgroundWorker to process your DB queries and call ProgressChanged to update the GUI.


In your code you hit the database inside while loop, that cause your program hung. So you can create a big update query and call Command.Execute once. this may not be the best solution but it will save the time of opening and closing database connection for each iteration.

StringBuilder updateQuery = new StringBuilder("");
while (rdr.Read())
{
   temp = (rdr[harltabcol[i, 0]].ToString()).ToString();
   temp1 = (Iscii2Unicode(temp)).ToString();
   updateQuery.Append("UPDATE [" + harltabcol[i,1] + "] SET [" + harltabcol[i,0] + "] =N'"+temp1+"' WHERE " + harltabcol[i,0] + "='" + temp + "';");
} 
SqlCommand cmdUpd = new SqlCommand(updateQuery.ToString(),cn1);
cn1.Open();
cmdUpd.CommandTimeout = 0;
cmdUpd.ExecuteNonQuery();
temp = null;
temp1 = null;
cn1.Close();
cn.Close(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号