111,098
社区成员




public List<object> GetEntity(string table, string[] fieldNames, string[] values)
{
List<object> obj = null;
if (fieldNames.Length != values.Length)
{
return null;
}
string query = "From " + table + " as table Where 1=1 ";
for (int index = 0; index < fieldNames.Length; index++)
{
query += " and table." + fieldNames[index] + " =? ";
}
ISession session = SessionFactory.OpenSession(_AssemblyName);
ITransaction transaction = session.BeginTransaction();
IQuery querySession = session.CreateQuery(query);
for (int index = 0; index < fieldNames.Length; index++)
{
querySession.SetString(index, values[index]);
}
obj = (List<object>)querySession.List<object>();
transaction.Commit();
session.Close();
return obj;
}
static void Main(string[] args)
{
Console.WriteLine(GetEntity("table1", new string[] { "column1", "column2" }, new string[] { "value1", "value2" }));
}
static string GetEntity(string tableName, string[] columns, string[] values)
{
if (columns.Length != values.Length)
return "";
if (columns == null || values == null || columns.Length == 0)
return "select * from " + tableName;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < columns.Length; i++)
sb.Append(columns[i] + "='" + values[i] + "' and ");
return "select * from " + tableName + " where " + sb.ToString().Substring(0, sb.ToString().LastIndexOf("and"));
}