关键字“INTO”附近的语法不正确

时间:2015-08-13 09:50:26

标签: sql asp.net sql-server sql-insert

这是导致错误的insert查询:

 public int Insert(User obj){

     string query = "INSERT IGNORE INTO tblUser(Name,Photo,Email,Password,Branch,Phone,Address,Authority,Recover_Email,Facebook_ID)  values(@name,@photo,@email,@pass,@branch,@phone,@addr,@auth,@re,@fbID) ";
     List<SqlParameter> lstp = new List<SqlParameter>();
     lstp.Add(new SqlParameter("@name", obj.Name));
     lstp.Add(new SqlParameter("@photo", obj.Photo));
     lstp.Add(new SqlParameter("@email", obj.Email));
     lstp.Add(new SqlParameter("@pass", obj.Password));
     lstp.Add(new SqlParameter("@branch", obj.Branch));
     lstp.Add(new SqlParameter("@phone", obj.Phone));
     lstp.Add(new SqlParameter("@addr", obj.Address));
     lstp.Add(new SqlParameter("@auth", obj.Authority));
     lstp.Add(new SqlParameter("@re", obj.Recovery_Email));
     lstp.Add(new SqlParameter("@fbID", obj.Facebook_ID));
     return DBUtility.ModifyData(query, lstp);
}

例外详细信息:

  

System.Data.SqlClient.SqlException:关键字附近的语法不正确   'INTO'

表定义:

CREATE TABLE [dbo].[tblUser] (
    [UserID]         INT           IDENTITY (1, 1) NOT NULL,
    [Name]           VARCHAR (100) NULL,
    [Photo]          VARCHAR (500) NULL,
    [Email]          VARCHAR (50)  NULL,
    [Password]       VARCHAR (500) NULL,
    [Branch]         VARCHAR (50)  NULL,
    [Phone]          VARCHAR (50)  NULL,
    [Address]        VARCHAR (500) NULL,
    [Recovery_Email] VARCHAR (100) NULL,
    [Facebook_ID]    VARCHAR (200) NULL,
    PRIMARY KEY CLUSTERED ([UserID] ASC)
);

如果需要,堆栈跟踪:

 [SqlException (0x80131904): Incorrect syntax near the keyword 'INTO'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1787814
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5341674
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1693
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +275
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +208
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
   DataAccess.DBUtility.ModifyData(String query, List`1 lstParams) in c:\Users\ABHISHEK\Documents\Visual Studio 2013\Projects\InventoryManagement\DataAccess\DBUtility.cs:36
   BusinessLogic.UserLogic.Insert(User obj) in c:\Users\ABHISHEK\Documents\Visual Studio 2013\Projects\InventoryManagement\BusinessLogic\UserLogic.cs:30
   InventoryManagementSys.FacebookSync.AuthorizeUser() in c:\Users\ABHISHEK\Documents\Visual Studio 2013\Projects\InventoryManagement\InventoryManagementSys\FacebookSync.aspx.cs:83
   InventoryManagementSys.FacebookSync.Page_Load(Object sender, EventArgs e) in c:\Users\ABHISHEK\Documents\Visual Studio 2013\Projects\InventoryManagement\InventoryManagementSys\FacebookSync.aspx.cs:20
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

2 个答案:

答案 0 :(得分:2)

AFAIK,您无法将INSERT IGNORE用于SQL SERVER。如果您在SQL SERVER 2008 onwards,则可以MERGE使用INSERT(如果行不存在)或UPDATE。 e.g。

MERGE
INTO    tmp_final dv
USING   tmp_holding_DataValue t
ON      t.dateStamp = dv.dateStamp
        AND t.itemId = dv.itemId
WHEN NOT MATCHED THEN
INSERT  (dateStamp, itemId, value)
VALUES  (dateStamp, itemId, value)

编辑:很明显,您正在针对SQL Server执行,因为SQL Server客户端特定的类型和方法出现在堆栈跟踪中。

答案 1 :(得分:0)

基于对问题和答案的评论:

你有Microsoft's SQL Server RDBMS混淆了Oracle's MySQL RDBMS。

使用SQL Server时需要使用T-SQL(Microsoft的SQL方言)。

使用MySQL时需要使用MySQL的SQL方言。

虽然可以编写使用两者的公共代码,但它要复杂得多(例如,您需要使用常见类型(如DbCommand)来生成具有某种形式工厂层的正确具体类型的大多数代码)。

使用一个RDBMS(更简单的选项)或构建基础结构(包括限制自己使用SQL方言的公共部分)以跨RDBMS工作。