这是我第一次使用nhibernate,我在连接和检索PostgreSQL数据库时遇到了问题。代码没有报错,但也没有返回任何值。当我使用pgAdmin3时,可以得到数据。
AutoPersistenceModel model = AutoMap.Assembly(System.Reflection.Assembly.GetCallingAssembly())
.Where(t => t.Namespace == "POCPostgresql.Model");
var configuration = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard
.ConnectionString(c => c
.Host("server")
.Port(5432)
.Database("database")
.Username("username")
.Password("password")))
.Mappings(m => m
.AutoMappings.Add(model))
.ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
.BuildSessionFactory();
using (var session = configuration.OpenSession())
{
// Query all objects
var completeList = session.CreateCriteria<Object>().List();
Console.ReadLine();
}
completelist变量是一个空列表。
我是否忘记了什么?
编辑: 当将配置移动到app.config并以不同的方式初始化时,结果为空列表。
Configuration configuration = new Configuration();
configuration.Configure();
ApplicationCore.Instance.SessionFactory = configuration.BuildSessionFactory();
using (var session = ApplicationCore.Instance.SessionFactory.OpenSession())
{
var completeList = session.CreateCriteria<Object>().List();
Console.ReadLine();
}
编辑2: 我认为可能是服务器因某种原因拒绝了我的查询,但我已尝试仅使用npgsql进行查询,这很正常。
NpgsqlConnection conn = new NpgsqlConnection("server=server;Port=5432;Database=database;User Id=username;Password=password;");
conn.Open();
string sql = "SELECT * FROM report LIMIT 100";
NpgsqlCommand command = new NpgsqlCommand(sql, conn);
NpgsqlDataReader dr = command.ExecuteReader();
while (dr.Read())
Console.Write("{0}\t{1} \n", dr[0], dr[1]);
conn.Close();
Console.ReadLine();