【发布时间】:2014-02-04 15:42:05
【问题描述】:
我尝试在两个条件下进行 Select。 这是我的功能:
public static List<T> Select_mult(List<string> list, string op)
{
List<T> liste = new List<T>();
using (MySqlConnection connexion = new MySqlConnection("Data Source=localhost;Initial Catalog=dev_ing2;User Id=root ;Password='';"))
{
Type type = typeof(T);
connexion.Open();
MySqlTransaction transaction = connexion.BeginTransaction();
string req = "SELECT * FROM " + type.Name + " WHERE (";
MySqlCommand command = new MySqlCommand();
foreach (PropertyInfo p in type.GetProperties())
{
if (p.Name == "Id")
break;
foreach (string value in list)
{
req += p.Name.ToLower() + " = ";
req += value + " OR ";
}
req = req.Substring(0, req.Length - 3);
req += ")" + op +"(" ;
}
req = req.Substring(0, req.Length - 5);
Console.WriteLine(req);
Console.ReadLine();
command.Connection = connexion;
command.Transaction = transaction;
command.CommandText = req;
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
T user = new T();
foreach (PropertyInfo p in type.GetProperties())
{
p.SetValue(user, reader[p.Name]);
}
liste.Add(user);
}
}
return liste;
}
}
我在“where 子句”中有一个错误未知列“Chassot”。 “Chassot”是我的 sql 表中用户的姓氏
感谢帮助。 =)
【问题讨论】:
-
检查列表中的值?
-
发布
Console.WriteLine(req)输出的内容 -
我建议使用经过全面测试的 ORM,例如 Dapper
-
是您在
type.properties"chassot"的物业之一? -
您的语法在 Where 子句中是关闭的。要么比,要么您尝试引用该实例中不存在的列。