2011-08-29 42 views
0

以下應用程序代碼在更新數據庫記錄時掛起。 它只在輸出屏幕上顯示0,並且程序永遠掛起。 但是表中有超過50,000條記錄。 (程序代碼ISCII轉換爲Unicode並寫入到數據庫。)爲什麼我的應用程序在更新數據庫記錄時掛起?

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; 
        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) 
    { 

    } 
} 
} 

回答

4

正常。你不會給消息循環一個運行的機會。使用BackgroundWorker來處理您的數據庫查詢並呼叫ProgressChanged來更新GUI。

+0

關於[WinForms Threading](http://www.yoda.arachsys.com/csharp/threads/winforms.shtml)的一個很好的資源,特別是關於UI線程的部分。這應該有助於你理解它爲什麼會掛起,以及Serge爲什麼會修復它。 –

1

在你的代碼中,你點擊while循環中的數據庫,導致你的程序掛起。因此,您可以創建一個大的update查詢並呼叫Command.Execute一次。這可能不是最好的解決方案,但它會節省每次迭代打開和關閉數據庫連接的時間。

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

Bu連接cn和cn1必須在while循環之前打開,讀取和更新應在循環中運行,並且必須關閉循環cn和cn1 – icaptan

相關問題