我目前正在尝试使NHibernate与SAP的Advantage Database Server 12一起使用。不幸的是,Internet上该数据库服务器周围没有太多信息,但是在这种情况下,我必须使用它来访问现有数据库。
因此,我尝试首先建立一个简单的测试项目,该项目将“学生”添加到“学生”表中,如下:https://www.tutorialspoint.com/nhibernate/nhibernate_basic_orm.htm
关于ADS,我在这里找到了驱动程序和方言:https://groups.google.com/forum/#!topic/nhibernate-development/s7HLs-aEXDk
我已经建立了一个表格和以下代码:
private void button1_Click(object sender, EventArgs e)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = "data source=C:\\Users\\xxx\\Downloads;ServerType=local; TableType=ADT; Shared=True; LockMode=COMPATIBLE";
x.Driver<AdvantageDatabaseServerDriver>();
x.Dialect<AdvantageDatabaseServer8Dialect>();
x.LogSqlInConsole = true;
});
cfg.AddAssembly(Assembly.GetExecutingAssembly());
var sefact = cfg.BuildSessionFactory();
using (var session = sefact.OpenSession())
{
using (var tx = session.BeginTransaction())
{
//perform database logic
tx.Commit();
}
Console.ReadLine();
}
using (var session = sefact.OpenSession())
{
using (var tx = session.BeginTransaction())
{
var student1 = new Students
{
ID = 1,
FirstMidName = "Allan",
LastName = "Bommer"
};
var student2 = new Students
{
ID = 2,
FirstMidName = "Jerry",
LastName = "Lewis"
};
session.Save(student1);
session.Save(student2);
tx.Commit();
}
Console.ReadLine();
}
}
但是在第一次写尝试时,它总是抛出此异常:
NHibernate: INSERT INTO C:\Users\ad\Downloads\REFERENCE\Students.adt (LastName, FirstMidName) VALUES (?, ?); select NEWIDSTRING() from system.iota;p0 = 'Glenn' [Type: String (-1:0:0)], p1 = 'Allan' [Type: String (-1:0:0)]
Exception thrown: 'Advantage.Data.Provider.AdsException' in NHibernate.dll
方言可能有什么问题吗?非常感谢您的帮助,谢谢!
答案 0 :(得分:0)
根据评论,问题是这样的:
NHibernate与您使用的语法一起发出以下代码:
INSERT INTO
C:\Users\ad\Downloads\REFERENCE\Students.adt
(
LastName
, FirstMidName
)
VALUES
(
?
, ?
);
select NEWIDSTRING() from system.iota;
显然,用于ADS的ASP.NET驱动程序无法正确处理这种形式的参数传递。
您链接的knowledge base item建议将SQL改为使用命名参数:
INSERT INTO
C:\Users\ad\Downloads\REFERENCE\Students.adt
(
LastName
, FirstMidName
)
VALUES
(
:p0
, :p1
);
select NEWIDSTRING() from system.iota;
我对NHibernate的了解不足,无法告诉您如何实现这一目标。