大家好!
我问了一次熟悉的问题,现在又被卡住了
什么是重构这个的更好方法?
PS。不希望将查询作为参数传递,搜索最优雅的解决方案
方法1
public static List<string> GetWinners(Prize prize)
{
List<string> winners = new List<string>();
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString))
{
var query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.WinnerID ",
"AND PrizeID = " + prize.ID
);
connection.Open();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbDataReader reader = command.ExecuteReader();
...
connection.Close();
}
return winners;
}
方法2
public static List<string> GetDoubles(Prize prize)
{
List<string> doubles = new List<string>();
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString))
{
var query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.DoubleID ",
"AND PrizeID = " + prize.ID
);
connection.Open();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbDataReader reader = command.ExecuteReader();
...
connection.Close();
}
return doubles;
}
答案 0 :(得分:0)
使用String.Format
并将propname作为参数传递:
public static List<string> GetSomething(string propName, Prize prize)
{
List<string> doubles = new List<string>();
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString))
{
var query = string.Format(
@"SELECT Member.IBAN,
FROM Member, Summary,
WHERE Member.ID = Summary.{0},
AND PrizeID = {1}", propName, prize.ID
);
connection.Open();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbDataReader reader = command.ExecuteReader();
...
connection.Close();
}
return doubles;
}
顺便说一句 - 请注意SQLI&#39; s - 阅读parameterized queries
。
答案 1 :(得分:0)
好吧,你可以传递你想要的类型作为枚举,并阅读它。 它看起来像这样:
enum Target { DOUBLES, WINNERS } // add as many as you want.
public static List<string> GetTarget(Prize prize, Target lookingFor)
{
....
// define the query
switch(lookingFor) // take care of each case in the enum.
{
case Target.DOUBLES:
query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.DoubleID ",
"AND PrizeID = " + prize.ID
);
break;
case Target.WINNERS:
query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.WinnerID ",
"AND PrizeID = " + prize.ID
);
break;
default: // in case you got a bad value
//throw an exception
break;
}
//Do stuff with the query.
}
答案 2 :(得分:0)
方法1
public static List<string> GetWinners(Prize prize)
{
List<string> winners = new List<string>();
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString))
{
var query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.WinnerID ",
"AND PrizeID = " + prize.ID
);
connection.Open();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbDataReader reader = command.ExecuteReader();
...
connection.Close();
}
return winners;
}
<强>方法2 强>
public static List<string> GetDoubles(Prize prize)
{
List<string> doubles = new List<string>();
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString))
{
var query = string.Concat(
@"
SELECT Member.IBAN ",
"FROM Member, Summary ",
"WHERE Member.ID = Summary.DoubleID ",
"AND PrizeID = " + prize.ID
);
connection.Open();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbDataReader reader = command.ExecuteReader();
...
connection.Close();
}
return doubles;
}
加入这些方法
protected void JoinMethods()
{
List<string> j = new List<string>();
j.AddRange(this.GetWinners("Parameters goes here"); //Method 2
j.AddRange(this.GetDoubles("Parameters goes here"); //Method 2
}
答案 3 :(得分:0)
我宁愿列举可能的案例(enum WinnerKind
),类似的事情:
public enum WinnerKind {
Winner = 0,
Double = 1
}
...
public static List<string> GetWinnersData(Prize prize, WinnerKind kind) {
List<string> result = new List<string>();
// You don't have to concat strings
// it's a good practice to use parametrized queries: "PrizeID = ?"
String query = String.Format(
@"SELECT Member.IBAN,
FROM Member,
Summary,
WHERE Member.ID = {0}
AND PrizeID = ?",
kind == WinnerKind.Winner ? "Summary.WinnerID" : "Summary.DoubleID");
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForteLotteryDataConnectionString"].ConnectionString)) {
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection)) {
// named parameters aren't supported
command.Parameters.Add(prize.ID);
using (OleDbDataReader reader = command.ExecuteReader()) {
while (reader.Read())
result.Add(Convert.ToString(reader[0].GetValue()));
}
}
}
return result;
}
public static List<string> GetWinners(Prize prize) {
return GetWinnersData(prize, WinnerKind.Winner);
}
public static List<string> GetDoubles(Prize prize) {
return GetWinnersData(prize, WinnerKind.Double);
}