2012-08-29 51 views
0

我正在做一個代碼,我有一個文本文件,其中包含10000多行唯一鍵(每個鍵在一個新行中)。我需要將密鑰與我提供的ProductID和ProductFeatures一起插入表中。StreamReader和StreamWriter在C語言中的while循環中嵌套#

我已經實現了這一點,通過使用下面的代碼來檢查表中已經存在的鍵並減少鍵的冗餘(表中沒有鍵兩次提供鍵)。

public void reader(string productid,string productfeature) 
{ 
    con.Open(); 
    using (StreamReader sr = new StreamReader(Server.MapPath("keys.txt"))) 
    { 
     while(sr.Peek() >=0) 
     { 
      string str = sr.ReadLine(); 
      SqlCommand cmd = new SqlCommand("select count(newkey) from serialkey where newkey ='"+str.ToString()+"'", con); 
      int keyvalue = Convert.ToInt32(cmd.ExecuteScalar()); 
      if (keyvalue == 0) 
      { 
       com = new SqlCommand("insert into SerialKey (productid,productfeatures,newkey) values ('" + productid.ToString() + "','" + productfeature.ToString() + "','" + key + "')", con); 
       com.ExecuteNonQuery(); 
      } 
     } 
    } 
    con.Close(); 
} 

這段代碼對我來說絕對好,但是我決定在這個過程中做一些修改。

  1. 只要鍵被插入到表中,鍵(行)應該從文本文件中刪除。

爲此,我想我必須在同一個while循環中使用StreamWriter/Textwriter,所以我嘗試使用此代碼。

using (StreamReader sr = new StreamReader(Server.MapPath("keys.txt"))) 
{ 
    while (sr.Peek() >= 0) 
    { 
     string str = sr.ReadLine(); 
     SqlCommand cmd = new SqlCommand("select count(newkey) from serialkey where newkey ='" + str.ToString() + "'", con); 
     int keyvalue = Convert.ToInt32(cmd.ExecuteScalar()); 

     if (keyvalue == 0) 
     { 
      string line = null; 
      sr.Close(); 
      TextWriter writer = new StreamWriter(Server.MapPath("keys.txt"), true); 
      if (insert(productid, productfeature, str)) 
      { 
       if (str != null) 
       { 
        writer.WriteLine(line); 
       } 
       writer.Flush(); 
       writer.Close(); 
      }      
     } 
    } 
} 

我想我已經使用了正確的邏輯刪除已插入的行。 如果我繼續的代碼行

sr.Close(); 

我收到以下錯誤

「不能從一個封閉的TextReader讀取。」

如果我刪除線

sr.Close(); 

我得到的錯誤

「該進程無法訪問該文件‘myfolderpath \ keys.txt’,因爲它 正由另一處理。」

是否有我可以在whileloop中使用StreamReader和StreamWriter的嵌套。最後,我必須刪除剛剛插入表格的行。

由於提前,

Viknesh

+0

爲什麼你需要在循環中「刪除」該行,如果你只是根據插入的鍵在最後重寫文件,它會不會更有效,更不容易出錯?順便說一句,你應該通過使用'using'語句來將連接放置在方法中,如果這是ASP.NET的話,這一切都是最重要的。 –

回答

0

的問題歸結爲你讀它寫入文件。

在某個最大可能的文件大小下面,最簡單的方法是首先將整個內容讀入內存,這樣您就可以使用一系列行。只需將添加的行添加到列表中,然後在關閉閱讀器後再寫入。

經過一定的大小後,會佔用太多內存,但可以寫入不同的文件,然後將其複製到舊文件中。

我會舉例,但其他代碼的問題意味着我不能完全弄清楚你在做什麼(line始終爲空,但正在寫入,str永遠不會爲空,但會檢查它是否爲)。

+0

這裏即時只是試圖寫一個空值的文本文件中的密鑰存在的特定地方。 Str不過是存儲在字符串中的關鍵值。 – Vikneshwar

+0

寫'null'對textwriter什麼都不做。我沒有看到'str'如何不再是'ReadLine()'的值,它不是null。然而'line'總是空的,所以你打開流來追加(最後添加東西),然後不做任何事情。或者,如果你打開它作爲不附加,最後你會有一個零字節的文件。 –

+0

Tats我想要做的。它不會打擾我,如果keys.txt文件沒有任何東西在裏面。即使它的零字節多數民衆贊成在我身上。換句話說,我可以說我只想用空值替換包含我的密鑰的行。 – Vikneshwar

0

我仍然無法對方法進行排序,以從keys.txt文件獲取內容,然後將密鑰插入到我的表中,然後從keys.txt文件中刪除行(其中存在密鑰)。 只要特定的鍵被插入到表格中,就應該從txt文件中刪除一個鍵,但不知何故,我已經排序了另一種方式,但我不認爲它的效率足夠高。

這裏我獲取所有的鍵並將它們存儲在一個數組中,然後在清除txt文件的內容之後使用foreach循環插入它們。

但我仍然喜歡以前的方法,由於其性能問題。然而任何人都可以嘗試這一塊代碼。

從文件keys.txt中獲取3000行密鑰然後將它們插入到表中需要8秒左右的時間延遲。

進出口仍然引用了源代碼下面幾行

using (StreamReader sr = new StreamReader(Server.MapPath("keys.txt"))) 
    { 

     string keys = sr.ReadToEnd(); 
     string[] allkeys = Regex.Split(keys, "\r\n"); 
     foreach (string values in allkeys) 
     { 
      SqlCommand cmd = new SqlCommand("select count(newkey) from serialkey where newkey ='" + values.ToString() + "'", con); 
      int keyvalue = Convert.ToInt32(cmd.ExecuteScalar()); 

      if (keyvalue == 0) 
      { 
       com = new SqlCommand("insert into SerialKey (productid,productfeatures,newkey) values ('" + productid.ToString() + "','" + productfeature.ToString() + "','" + key + "')", con); 
       com.ExecuteNonQuery(); 
      } 

     } 
     sr.Close(); 
     File.WriteAllText(Server.MapPath("keys.txt"), string.Empty); 
} 

希望這大塊的代碼可以幫助別人與同一類問題。這只是替代方式,而不是我的查詢的解決方案。