Reputation: 59
i have this this code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using base_donnee;
using System.IO;
namespace TrackingUnitSimulator
{
public class connection
{
public ISession session ;
public IList<simulateur> simulateurs = null;
public IList<user> users = null;
public IList<equipment> equipments = null;
public IList<string> jours = null;
public IList<int> conn_1 = null;
public IList<int> recep_1 = null;
public IList<int> envoi_1 = null;
public IList<int> conn_tout = null;
public IList<int> recep_tout = null;
public IList<int> envoi_tout = null;
public IList<Performance> performances = null;
public int[] mesures = new int[100];
public IList<int> nombres = null;
public connection()
{
ISessionFactory factory;
Configuration config = new Configuration();
config.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2005Dialect");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, "NHibernate.Driver.SqlClientDriver");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Simulation;Integrated Security=True;Pooling=False");
config.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
config.AddAssembly("base_donnee");
factory = config.BuildSessionFactory();
try
{
session = factory.OpenSession();
try
{
simulateurs = session.CreateQuery(@"select e from simulateur e ordred by e.Date").List<simulateur>();
users = session.CreateQuery(@"select e from user e ").List<user>();
}
catch (Exception ex) { File.AppendAllText(@"C:\Users\HP\Desktop\test.txt",ex.ToString());}
equipments = session.CreateQuery(@"select e from equipment e ").List<equipment>();
performances = session.CreateQuery(@"select e from Performance e ").List<Performance>();
jours = session.CreateQuery(@"select distinct e.Date from simulateur e ordred by e.Date").List<string>();
nombres = session.CreateQuery(@"select e.Nombre_simulateur from simulateur e ordred by e.Date").List<int>();
conn_1 = session.CreateQuery(@"select e.temps_connection from simulateur e where e.Nombre_simulateur = 1 ").List<int>();
recep_1 = session.CreateQuery(@"select e.temps_reception from simulateur e where e.Nombre_simulateur = 1 ").List<int>();
envoi_1 = session.CreateQuery(@"select e.temps_envoi from simulateur e where e.Nombre_simulateur = 1 ").List<int>();
conn_tout = session.CreateQuery(@"select e.temps_connection from simulateur e ").List<int>();
recep_tout = session.CreateQuery(@"select e.temps_reception from simulateur e ").List<int>();
envoi_tout = session.CreateQuery(@"select e.temps_envoi from simulateur e ").List<int>();
int i = 0;
foreach (string j in jours) {
IQuery query = session.CreateQuery(@"select e from simulateur e where e.Date=:j ");
query.SetString("j", j);
IList<simulateur> med = query.List<simulateur>();
mesures[i] = med.Count;
i++;
}
}
catch
{
session.Close();
}
}
public ISession getSession(){
return session;
}
}
}
and the class user.cs :
using System;
using System.Collections.Generic;
using System.Text;
namespace base_donnee
{
public class user
{
public virtual string login { get; set; }
public virtual string password { get; set; }
public virtual string name { get; set; }
public virtual int age { get; set; }
public virtual string location { get; set; }
}
}
and the class user.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="base_donnee">
<class name="base_donnee.user, base_donnee" table="user">
<id name="login" type="string"></id>
<property name="password" type="string" />
<property name="age" type="int" />
<property name="location" type="string" />
<property name="name" type="string" />
</class>
</hibernate-mapping>
my problem is that an exception appears:
NHibernate.ADOException: could not execute query
[ select user0_.login as login3_, user0_.password as password3_, user0_.age as age3_, user0_.location as location3_, user0_.name as name3_ from user user0_ ]
[SQL: select user0_.login as login3_, user0_.password as password3_, user0_.age as age3_, user0_.location as location3_, user0_.name as name3_ from user user0_] ---> System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.
i need some ideas how can i avoid this error ?and why the table user ?
Upvotes: 0
Views: 1206
Reputation: 241641
user
is a keyword in T-SQL. Because you've named your table user
, it is clashing with the T-SQL keyword.
You need to set auto quote:
config.Properties["keywords"] = "auto-quote";
Beyond that, your code is an absolute disaster. You're swallowing the top-level Exception
type. You have hard-coded SQL statements but you're using NHibernate (this is rarely necessary). You're trying to close an session after it possibly failed to open. You iterate over a list using foreach
but you have a manually managed index variable i
. I could go on for awhile.
Upvotes: 6