2016-07-30 228 views
0

我試圖做的是更新條目,如果不存在的話 - 創建一個新的附近有語法錯誤('附近「=」

This是我的東西。試圖跟隨

我得到一個語法錯誤例外,我不知道什麼是錯的


這就是如何使我的表:。

CREATE TABLE [dbo].[Rides] (
    [phone]  VARCHAR (32) NOT NULL, 
    [destination] VARCHAR (50) NOT NULL, 
    [departure] VARCHAR (50) NOT NULL, 
    [time]  DATETIME  NOT NULL, 
    [comment]  NVARCHAR (50) NOT NULL, 
    PRIMARY KEY CLUSTERED ([phone] ASC) 
); 

這是我的查詢:

SqlCommand command = connection.CreateCommand(); 
command.CommandText = 
@"UPDATE Rides SET ([email protected], [email protected], [email protected], [email protected]) WHERE [email protected] 
IF (@@ROWCOUNT=0) 
    INSERT INTO Rides VALUES ([email protected], [email protected], [email protected], [email protected])"; 

command.Parameters.AddWithValue("@UName", entry.phone); 
command.Parameters.AddWithValue("@Dest", entry.destinationID); 
command.Parameters.AddWithValue("@Depart", entry.departureID); 
command.Parameters.AddWithValue("@Time", entry.time.ToString("yyyy-MM-dd HH:mm:ss:fff")); 
command.Parameters.AddWithValue("@Comment", entry.comment); 
command.ExecuteNonQuery(); 

這是entry

public struct Entry 
{ 
    public string phone; 
    public string destinationID; 
    public string departureID; 
    public DateTime time; 
    public string comment; 
} 

這是我的錯誤:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near '('.

Incorrect syntax near '='.

堆棧跟蹤:

[SqlException (0x80131904): Incorrect syntax near '('. 
Incorrect syntax near '='.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2442126 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5736904 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628 
    System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731 
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +225 
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375 
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +337 
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280 
    RidesDatabaseAccessor.updateEntry(Entry entry) in c:\Users\Climax708\Documents\Programming\TrempLehayal\App_Code\RidesDatabaseAccessor.cs:145 
    newride.Page_Load(Object sender, EventArgs e) in c:\Users\Climax708\Documents\Programming\TrempLehayal\newride.aspx.cs:75 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 
    System.Web.UI.Control.OnLoad(EventArgs e) +95 
    System.Web.UI.Control.LoadRecursive() +59 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952 

回答

3

我所做的弗洛翼變化:

  1. 我加了你的兩個報表(以下簡稱UPDATE和 的INSERT)之間的分號。
  2. 我刪除括號從UPDATE聲明(他們不是 需要)
  3. 我糾正你的INSERT語句的語法。列名 必須在VALUES之前在其自己的括號聲明 中單獨給出。

    command.CommandText = @"UPDATE Rides 
        SET [email protected], [email protected], [email protected], [email protected] 
        WHERE [email protected]; 
        IF (@@ROWCOUNT=0) 
         INSERT INTO Rides (destination, departure, time, comment) 
         VALUES (@Dest, @Depart, @Time, @Comment)"; 
    
+2

雖然你已經正確地發現了這個問題,但我認爲你應該告訴OP你已經改變了什麼,否則他會冒險放鬆幾小時以找出差異。 – Steve

+0

謝謝!我添加了一個解釋。 –

+1

只是一點點修復。在插入的情況下,他還需要插入電話號碼 – Steve

-1

我想你應該嘗試

@「UPDATE遊樂設施設置目的地= @目的地,出發= @出發,時間= @時間,評論= @評論WHERE電話= @ UNAME

insted的您更新查詢

+1

這不能解決OP的問題。他們想要插入一個新行,如果一個不存在。 –

0

在你更新你一定不是()繞柱使用的集合

UPDATE Rides 
SET destination = @Dest, 
    departure = @Depart, 
    time = @Time, 
    comment = @Comment 
WHERE phone = @UName