0

我有这个代码:

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;
        }
    }
}

和类 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; }
    }
}

和类 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>

我的问题是出现异常: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:关键字“用户”附近的语法不正确。
我需要一些想法如何避免这个错误?为什么是表用户?

4

1 回答 1

6

user是 T-SQL 中的关键字。因为您已经命名了 table user,所以它与 T-SQL 关键字发生冲突。

您需要设置自动报价

config.Properties["keywords"] = "auto-quote";

除此之外,您的代码绝对是一场灾难。您正在吞噬顶级Exception类型。您有硬编码的 SQL 语句,但您使用的是 NHibernate(这很少需要)。您试图在会话可能无法打开后关闭它。foreach您使用但您有一个手动管理的索引变量遍历列表i。我可以继续一段时间。

于 2012-05-05T18:06:55.767 回答