我試圖做的是更新條目,如果不存在的話 - 創建一個新的附近有語法錯誤('附近「=」
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
雖然你已經正確地發現了這個問題,但我認爲你應該告訴OP你已經改變了什麼,否則他會冒險放鬆幾小時以找出差異。 – Steve
謝謝!我添加了一個解釋。 –
只是一點點修復。在插入的情況下,他還需要插入電話號碼 – Steve