我正在爲Grepolis創建一個應用程序,在線戰爭遊戲可與其他數千名玩家一起玩。我連接到http://en12.grepolis.com/data/players.txt的文本文件並將其下載到WebClient.DownloadString中。然後,我將它分解爲「\ n」,並將這些字符串中的每一個拆分爲「,」。每行包含玩家ID,名稱,alliance_id,積分,等級和城鎮(玩家所在城鎮的數量)。最後一次計數超過60000。我在大約10分鐘內將他們全部上傳到了本地數據庫,現在正在對每一行進行更新,以便在此過程中刪除所有0分的玩家,並且自上次更新後更改的人都會更新。問題是,即使只是更新已經超過10分鐘到目前爲止。C#將數千行從文本文件快速添加到數據庫
有沒有真正快速的方法來做我想做的事情?這是所有我認爲這是相關的代碼:
文件:Global.asax.cs中
protected void Application_Start(object sender, EventArgs e)
{
WebClient wc = new WebClient();
String str = wc.DownloadString("http://en12.grepolis.com/data/players.txt");
String[] players = str.Split('\n');
foreach (String player in players)
{
String[] playerInfo = player.Split(',');
ContentManager cm = new ContentManager();
int id = 0;
String name = "";
int alliance_id = 0;
int points = 0;
int rank = 0;
int towns = 0;
try
{
int.Parse(playerInfo[0]);
id = int.Parse(playerInfo[0]);
}
catch
{
}
try
{
name = playerInfo[1];
}
catch
{
}
try
{
int.Parse(playerInfo[2]);
alliance_id = int.Parse(playerInfo[2]);
}
catch
{
}
try
{
int.Parse(playerInfo[3]);
points = int.Parse(playerInfo[3]);
}
catch
{
}
try
{
int.Parse(playerInfo[4]);
rank = int.Parse(playerInfo[4]);
}
catch
{
}
try
{
int.Parse(playerInfo[5]);
towns = int.Parse(playerInfo[5]);
}
catch
{
}
if (points > 0 && towns > 0 && id > 0 && !name.Equals(""))
{
try
{
cm.UpdatePlayer(id, name, alliance_id, points, rank, towns);
}
catch
{
}
}
else
{
try
{
cm.DeletePlayer(id);
}
catch
{
}
}
}
}
文件:ContentManager.cs
public class ContentManager : IHttpModule
{
SqlDataSource conn = new SqlDataSource(WebConfigurationManager.ConnectionStrings["DevConn"].ConnectionString, "");
public void UpdatePlayer(int id, String name, int alliance_id, int points, int rank, int towns) {
conn.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
conn.UpdateCommand = "UpdatePlayer";
conn.UpdateParameters.Clear();
conn.UpdateParameters.Add("id", id.ToString());
conn.UpdateParameters.Add("name", name);
conn.UpdateParameters.Add("alliance_id", alliance_id.ToString());
conn.UpdateParameters.Add("points", points.ToString());
conn.UpdateParameters.Add("rank", rank.ToString());
conn.UpdateParameters.Add("towns", towns.ToString());
conn.Update();
}
public void DeletePlayer(int id)
{
conn.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
conn.DeleteCommand = "DeletePlayer";
conn.DeleteParameters.Clear();
conn.DeleteParameters.Add("id", id.ToString());
conn.Delete();
}
}
謝謝。我試圖使用這個,但我使用變量設置文件名(通過參數輸入到更新過程中)。我正在使用Server.MapPath(「/ App_Data/players.txt」),但它在「C:」上拋出一個錯誤。我認爲這是導致問題的冒號。嘗試使用URL和HTML編碼無濟於事。任何想法是什麼造成這種情況? – ClarkeyBoy 2011-02-15 21:49:23