我尝试做的是更新一个条目,如果它不存在 - 创建一个条目。
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 (destination=@Dest, departure=@Depart, time=@Time, comment=@Comment) WHERE phone=@UName
IF (@@ROWCOUNT=0)
INSERT INTO Rides VALUES (destination=@Dest, departure=@Depart, time=@Time, comment=@Comment)";
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;
}
这是我的错误:
类型' System.Data.SqlClient.SqlException'的异常发生在System.Data.dll中但未在用户代码中处理
其他信息:'('。
附近的语法不正确' ='。
附近的语法不正确
堆栈追踪:
[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
答案 0 :(得分:3)
我做了以下更改:
UPDATE
和}之间添加了一个分号
INSERT
)。UPDATE
声明中删除了括号(它们不是
需要)我更正了INSERT
语句的语法。列名称
必须在他们自己的括号中单独给出
在VALUES
之前。
command.CommandText = @"UPDATE Rides
SET destination=@Dest, departure=@Depart, time=@Time, comment=@Comment
WHERE phone=@UName;
IF (@@ROWCOUNT=0)
INSERT INTO Rides (destination, departure, time, comment)
VALUES (@Dest, @Depart, @Time, @Comment)";
答案 1 :(得分:0)
在您的更新中,您不得使用set
列周围的()UPDATE Rides
SET destination = @Dest,
departure = @Depart,
time = @Time,
comment = @Comment
WHERE phone = @UName
答案 2 :(得分:-1)
我想你应该试试
@" UPDATE Rides SET destination = @ Dest,departure = @Dispart,time = @ Time,comment = @ Comment WHERE phone = @ UName
发布您的更新查询