我一直使用数据库SQL Server开发ASP.net程序,但是今天却被ACCESS数据库折磨了。

使用ASP.net向ACCESS数据库中插入数据,相当简单的代码。

一开始是出现“操作必须使用一个可更新的查询”的错误,将存放ACCESS数据库的目录(需要对ACCESS数据库所在的目录)加上“Network”和“Network Service”的写入和修改的权限,解决!

随后的错误"INSERT INTO 语句的语法错误"就让我摸不着头脑了,Google了一些资料后才发现原来是我的ACCESS数据表的字段名使用了SQL的保留关键字。

将插入数据的SQL语句更改为:"insert into users([username],[desc]) values(‘"+username+ "’,'"+desc+"’)",即解决"INSERT INTO 语句的语法错误"的问题.

将保留关键字加上中括号([]).

 

Asp.net连接Access并更新数据库的代码
  1. using System;   
  2. using System.Collections;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Data.OleDb;   
  6. using System.Drawing;   
  7. using System.Web;   
  8. using System.Web.SessionState;   
  9. using System.Web.UI;   
  10. using System.Web.UI.WebControls;   
  11. using System.Web.UI.HtmlControls;   
  12.   
  13. namespace User   
  14. {   
  15.     /// <summary>   
  16.     /// xr 的摘要说明。   
  17.     /// </summary>   
  18.     public class CreateUser : System.Web.UI.Page   
  19.     {   
  20.         protected System.Web.UI.WebControls.TextBox TBUsername;   
  21.         protected System.Web.UI.WebControls.TextBox TBDesc;   
  22.         protected System.Web.UI.WebControls.Label LabelState;   
  23.         protected System.Web.UI.WebControls.Button BTNCreateUser;   
  24.        
  25.         private void Page_Load(object sender, System.EventArgs e)   
  26.         {   
  27.             // 在此处放置用户代码以初始化页面   
  28.         }   
  29.         private void BTNCreateUser_Click(object sender, System.EventArgs e)   
  30.         {   
  31.             string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" + Server.MapPath("mdb/database.mdb");   
  32.   
  33.             string username = this.TBUsername.Text.ToString();   
  34.             string desc = this.TBDesc.Text.ToString();   
  35.   
  36.             string strSql = "insert into users([username],[desc]) values(‘"+username+ "’,'"+desc+"’)";   
  37.   
  38.             System.Data.OleDb.OleDbConnection oleDbConnection = new System.Data.OleDb.OleDbConnection(ConnStr);   
  39.             System.Data.OleDb.OleDbCommand oleDbCommand = new System.Data.OleDb.OleDbCommand(strSql, oleDbConnection);   
  40.   
  41.             try  
  42.             {   
  43.                 oleDbCommand.Connection.Open();   
  44.                 LabelState.Text ="Access数据库连接状态:" + oleDbConnection.State;   
  45.                 oleDbCommand.ExecuteNonQuery();   
  46.             }   
  47.             catch(Exception ex)   
  48.             {   
  49.                 Response.Write(ex.Message.ToString());   
  50.             }   
  51.             finally  
  52.             {   
  53.                 oleDbCommand.Connection.Close();   
  54.             }   
  55.         }  
  56.         #region Web 窗体设计器生成的代码   
  57.             //代码省略  
  58.         #endregion   
  59.     }  

以下为SQL中的保留关键字,大家在设计数据表时,尽量不要使用如下单词作为字段名称:

SQL中的保留字
  1. action   add   aggregate   all  
  2. alter   after   and   as  
  3. asc   avg   avg_row_length   auto_increment   
  4. between   bigint   bit   binary  
  5. blob   bool   both   by  
  6. cascade   case   char   character  
  7. change   check   checksum   column  
  8. columns   comment   constraint   create  
  9. cross   current_date   current_time   current_timestamp  
  10. data   database   databases   date  
  11. datetime   day   day_hour   day_minute   
  12. day_second   dayofmonth   dayofweek   dayofyear   
  13. dec   decimal   default   delayed   
  14. delay_key_write   delete   desc   describe   
  15. distinct   distinctrow   double   drop  
  16. end   else   escape   escaped   
  17. enclosed   enum   explain   exists   
  18. fields   file   first   float  
  19. float4   float8   flush   foreign  
  20. from   for   full   function  
  21. global   grant   grants   group  
  22. having   heap   high_priority   hour  
  23. hour_minute   hour_second   hosts   identified   
  24. ignore   in   index   infile   
  25. inner   insert   insert_id   int  
  26. integer   interval   int1   int2   
  27. int3   int4   int8   into  
  28. if   is   isam   join  
  29. key   keys   kill   last_insert_id   
  30. leading   left   length   like  
  31. lines   limit   load   local  
  32. lock   logs   long   longblob   
  33. longtext   low_priority   max   max_rows   
  34. match   mediumblob   mediumtext   mediumint   
  35. middleint   min_rows   minute   minute_second   
  36. modify   month   monthname   myisam   
  37. natural   numeric   no   not  
  38. null   on   optimize   option  
  39. optionally   or   order   outer  
  40. outfile   pack_keys   partial   password  
  41. precision   primary   procedure   process   
  42. processlist   privileges   read   real  
  43. references   reload   regexp   rename   
  44. replace   restrict   returns   revoke  
  45. rlike   row   rows   second  
  46. select   set   show   shutdown   
  47. smallint   soname   sql_big_tables   sql_big_selects   
  48. sql_low_priority_updates   sql_log_off   sql_log_update   
  49. sql_select_limit   
  50. sql_small_result   sql_big_result   sql_warnings   straight_join   
  51. starting   status   string   table  
  52. tables   temporary   terminated   text   
  53. then   time   timestamp   tinyblob   
  54. tinytext   tinyint   trailing   to  
  55. type   use   using   unique  
  56. unlock   unsigned   update   usage   
  57. values   varchar   variables   varying  
  58. varbinary   with   write   when  
  59. where   year   year_month   zerofill