【发布时间】:2013-11-13 11:22:48
【问题描述】:
我在这里有一些 C# 示例,我实际上想知道如何优化它们。我想将代码示例减少到最低限度。有没有一种实用的方法来重构 Tour 类中的 GetDetails 或者我应该在 CombiTour 类中重构它?当然,我该如何重构它。
public class Tour
{
public string zielID;
public string ort;
public string strasse;
public string nummer;
public string plz;
public string land;
public string name;
public string fahrtNummer;
public Tour(string zielID)
{
this.zielID = zielID;
}
}
public class CombiTour
{
public List<Tour> touren;
public CombiTour()
{
this.touren = new List<Tour>();
}
public void GetDetails()
{
for (int i = 0; i < touren.Count; i++)
{
DataSet dsDetails = Tools.oGenericDs("SELECT Strasse, Nummer, PLZ, Ort, Land, Fahrtnummer FROM Ziele WHERE ZielID = " + this.touren[i].ZielID);
this.touren[i].Strasse = dsDetails.Tables[0].Rows[0]["Strasse"].ToString();
this.touren[i].Nummer = dsDetails.Tables[0].Rows[0]["Nummer"].ToString();
this.touren[i].PLZ = dsDetails.Tables[0].Rows[0]["PLZ"].ToString();
this.touren[i].Ort = dsDetails.Tables[0].Rows[0]["Ort"].ToString();
this.touren[i].Land = dsDetails.Tables[0].Rows[0]["Land"].ToString();
this.touren[i].Fahrtnummer = dsDetails.Tables[0].Rows[0]["Fahrtnummer"].ToString();
dsDetails = Tools.oGenericDs("SELECT Name FROM Kunden WHERE ZielID = " + this.touren[i].ZielID);
this.touren[i].Name = dsDetails.Tables[0].Rows[0]["Name"].ToString();
}
}
}
【问题讨论】:
-
不用其他工具也可以(见答案)。你不应该连接你的查询,因为这很容易受到 SQL 注入的攻击。
-
我还能如何将声明放在一起?
-
为此使用参数
标签: c# optimization coding-style dataset conventions