我用想OleDbCommand连接access,在一个using System;using System.Data;using system.Data.OleDb;的类中,定义了下面两个函数,可是总是出现“异常详细信息:System.Data.OleDb.OleDbException: FROM 子句语法错误。”是sql语句错了么?还有在这个类里不能调用Server.MapPath是怎么一回事啊?public static OleDbConnection createCon()
{
return new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data source=C:\Inetpub\wwwroot\person\mdb.mdb");
}
//判断用户名是否合法
public static bool judge(string UserName)
{
            OleDbConnection dbconn=conn.createCon();
dbconn.Open();
OleDbCommand cmd=new OleDbCommand("select * from user where username like '"+UserName+"'",dbconn);
            int count=Convert.ToInt32(cmd.ExecuteNonQuery());
if(count>0)
{
return true;
}
else
{
return false;
}
}

解决方案 »

  1.   

    conn,定义在哪里。
    静态方法createCon是conn的?
      

  2.   

    username 是关键字,把
    OleDbCommand cmd=new OleDbCommand("select * from user where username like '"+UserName+"'",dbconn);
    改为
    OleDbCommand cmd=new OleDbCommand("select * from [user] where [username] like '"+UserName+"'",dbconn);
      

  3.   

    Knight94(愚翁)按你说的改已经ok了,可是这是有什么区别么?
      

  4.   

    当然有区别,在Access中,有些名字是保留字,如果要在数据库或者某个字段中使用这些保留字的话,必须以“[]”来进行访问,否则的话,访问的是保留字(如果在语句没有什么问题的前提下),而不是你所要访问的表或字段。
      

  5.   

    再问一下,如下的代码运行时,为什么报错System.InvalidOperationException: 当传递具有新行的 DataRow 集合时,更新要求有效的 InsertCommand。Update()不是可以完成插入更新的功能么?
    OleDbConnection dbconn=conn.createCon();
    dbconn.Open();
    OleDbDataAdapter thisAdapter= new OleDbDataAdapter("select * from [user]",dbconn);
    DataSet thisDataSet=new DataSet();
    thisAdapter.Fill(thisDataSet,"usertable");
            DataRow thisRow=thisDataSet.Tables["usertable"].NewRow();
    thisRow["username"]=this.username.ToString().Trim();
    thisRow["sex"]=this.sex.ToString().Trim();
    thisRow["pwd"]=this.pwd.ToString().Trim();
    thisRow["question"]=this.question.ToString().Trim();
    thisRow["answer"]=this.answer.ToString().Trim();
    thisDataSet.Tables["usertable"].Rows.Add(thisRow);
    thisAdapter.Update(thisDataSet,"usertable");
    dbconn.Close();
      

  6.   


    OleDbConnection dbconn=conn.createCon();改为OleDbConnection dbconn = createCon();
      

  7.   

    你想用DataAdapter来把DataSet中的数据更新到数据库中,需要设置InsertCommand,UpdateCommand等等,参看
    http://blog.csdn.net/knight94/archive/2006/03/17/627556.aspx上面的例子虽说环境是在SQL Server,但是方法一样适用于Access数据库
      

  8.   

    实在想不通是哪里出问题,总是提示“System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。”
    OleDbConnection dbconn=conn.createCon();
    OleDbDataAdapter thisAdapter= new OleDbDataAdapter();
    thisAdapter.SelectCommand=new OleDbCommand("select * from [user]",dbconn);
    OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
    dbconn.Open();
    DataSet thisDataSet=new DataSet();
    thisAdapter.Fill(thisDataSet,"usertable");
            DataRow thisRow=thisDataSet.Tables["usertable"].NewRow();
    thisRow["username"]=this.username.ToString().Trim();
    thisRow["sex"]=this.sex.ToString().Trim();
    thisRow["pwd"]=this.pwd.ToString().Trim();
    thisRow["question"]=this.question.ToString().Trim();
    thisRow["answer"]=this.answer.ToString().Trim();
    thisDataSet.Tables["usertable"].Rows.Add(thisRow);
    thisAdapter.Update(thisDataSet,"usertable");
    dbconn.Close();
      

  9.   

    输出一下OleDbDataAdapter的InsertCommand,看看语句是不是你想要得
      

  10.   

    实在想不通是哪里出问题,很多人都说定义一个OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);就行了,可是我的还是不能实现,总是提示“System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误。”,问题到底在哪啊?