附近有语法错误('附近“=”
问题描述:
我试图做的是更新条目,如果不存在的话 - 创建一个新的附近有语法错误('附近“=”
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
答
我所做的弗洛翼变化:
- 我加了你的两个报表(以下简称
UPDATE
和 的INSERT
)之间的分号。 - 我删除括号从
UPDATE
声明(他们不是 需要) -
我纠正你的
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)";
答
我想你应该尝试
@“UPDATE游乐设施设置目的地= @目的地,出发= @出发,时间= @时间,评论= @评论WHERE电话= @ UNAME
insted的您更新查询
+1
这不能解决OP的问题。他们想要插入一个新行,如果一个不存在。 –
答
在你更新你一定不是()绕柱使用的集合
UPDATE Rides
SET destination = @Dest,
departure = @Depart,
time = @Time,
comment = @Comment
WHERE phone = @UName
虽然你已经正确地发现了这个问题,但我认为你应该告诉OP你已经改变了什么,否则他会冒险放松几小时以找出差异。 – Steve
谢谢!我添加了一个解释。 –
只是一点点修复。在插入的情况下,他还需要插入电话号码 – Steve