代码如下:
 str2 = "delete from user where userid=" +
              "'" + Int32.Parse(rolenameTextBox.Text)+"'";
        SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["nuwmsConnectionString"].ConnectionString);
        SqlCommand sda = new SqlCommand(str2, myconn);
        int nResultID = -1;
        try{
            myconn.Open();            nResultID = sda.ExecuteNonQuery();
        }
        catch(SqlException ex)
        {
           throw new Exception(ex.Message,ex);
           
        }
        finally{
            myconn.Close();
        }
错误信息:异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'user' 附近有语法错误。源错误: 
行 32:             myconn.Open();
行 33: 
行 34:             nResultID = sda.ExecuteNonQuery();
行 35:         }
行 36:         catch(SqlException ex)
   行: 34 堆栈跟踪: 
[SqlException (0x80131904): 在关键字 'user' 附近有语法错误。]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +177
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +68
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2300
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +191
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +380
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +115
   Delete.addButton_Click(Object sender, EventArgs e) in c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\Delete.aspx.cs:34[Exception: 在关键字 'user' 附近有语法错误。]
   Delete.addButton_Click(Object sender, EventArgs e) in c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\Delete.aspx.cs:38
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919我还有一个表,称为users,我将表换成users就没错,数据结构都相同,为什么换成user表就不行了?

解决方案 »

  1.   

    str2 = "delete user where userid=" +
                  "'" + Int32.Parse(rolenameTextBox.Text)+"'";
      

  2.   

    str2 = "delete from user where userid=" +
                  "'" + Int32.Parse(rolenameTextBox.Text)+"'";改為 str2 = "delete from [user] where userid=" +
                  "'" + Int32.Parse(rolenameTextBox.Text)+"'";
      

  3.   

    原因出在user那裡,因為user是MS SQL的關鍵字。
      

  4.   

    paoluo(一天到晚游泳的鱼)
    -----------------------------------
    谢谢,我还想多问一句,为什么加上[]就没有错误了?
      

  5.   

    fsjyclb() ( ) 信誉:100  2007-07-27 10:08:09  得分: 0  
     
     
       paoluo(一天到晚游泳的鱼)
    -----------------------------------
    谢谢,我还想多问一句,为什么加上[]就没有错误了?
      
     
    --------------
    MS SQL中就是這麼處理的。 :)如果你的表名中間有空格,比如Order Table就可以這麼查詢Select * From [Order Table]