using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoWebsite.DataAccess;
namespace VideoWebsite.Models
{
public class DatabaseModel
{
public int DatabaseID { get; set; }
public string DatabaseName { get; set; }
}
public class DatabaseModelAccess
{
public static IEnumerable<DatabaseModel> All()
{
string queryString = @"SELECT 1 as DatabaseID, 'test' as DatabaseName";
using (EntitiesModelA2 dbContext2 = new EntitiesModelA2())
{
IEnumerable<DatabaseModel> result = dbContext2.ExecuteQuery<DatabaseModel>(queryString); //Exception occurs here
return result;
}
}
}
}
我正在尝试实现非持久性类型。 我正在使用postgres数据库生成EntitiesModelA2。
答案 0 :(得分:1)
从Telerik Data Access角度来看,出现错误是因为查询没有FROM子句(它不符合the basic syntax for SELECT statements)。
你可以这样执行:
选项1
using (EntitiesModelA2 dbContext = new EntitiesModelA2())
{
using (IDbConnection connect = dbContext.Connection)
{
using (IDbCommand command = connect.CreateCommand())
{
command.CommandText = "SELECT 1 as DatabaseID, 'test' as DatabaseName FROM ExistingTableName";
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var result = new DatabaseModel();
result.DatabaseID = reader.GetInt32(0);
result.DatabaseName = reader.GetString(1);
//do something with the result
}
}
}
}
}
选项2
using (EntitiesModelA2 dbContext = new EntitiesModelA2())
{
var result = dbContext.ExecuteQuery<DatabaseModel>("SELECT 1 as DatabaseID, 'test' as DatabaseName FROM ExistingTableName");
}