C# 靠近'的语法不正确;(';和近';=';

C# 靠近'的语法不正确;(';和近';=';,c#,sql,sql-server,C#,Sql,Sql Server,我试图做的是更新一个条目,如果它不存在,创建一个新条目 这就是我想要遵循的 我得到一个语法错误异常,我不知道出了什么问题 这就是我制作桌子的方式: CREATE TABLE [dbo].[Rides] ( [phone] VARCHAR (32) NOT NULL, [destination] VARCHAR (50) NOT NULL, [departure] VARCHAR (50) NOT NULL, [time] DA

我试图做的是更新一个条目,如果它不存在,创建一个新条目

这就是我想要遵循的

我得到一个语法错误异常,我不知道出了什么问题


这就是我制作桌子的方式:

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();
这是
条目

public struct Entry
{
    public string phone;
    public string destinationID;
    public string departureID;
    public DateTime time;
    public string comment;
}
这是我的错误:

System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

其他信息:“(”附近的语法不正确

“=”附近的语法不正确

堆栈跟踪:

[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
    和 插入)
  • 我删除了
    UPDATE
    语句中的括号(它们不是 (需要)
  • 我更正了
    INSERT
    语句的语法 必须在他们自己的附加声明中单独给出 在
    值之前

    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)";
    

  • 我做了以下修改:

  • 我在你的两个语句之间加了一个分号(
    UPDATE
    和 插入)
  • 我删除了
    UPDATE
    语句中的括号(它们不是 (需要)
  • 我更正了
    INSERT
    语句的语法 必须在他们自己的附加声明中单独给出 在
    值之前

    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)";
    
  • 我想你应该试试

    @“更新乘车设置目的地=@Dest,出发=@Depart,time=@time,comment=@comment WHERE phone=@UName

    除了你的更新查询,我想你应该试试

    @“更新乘车设置目的地=@Dest,出发=@Depart,time=@time,comment=@comment WHERE phone=@UName


    在更新中,除了更新查询之外,您不能在列的周围使用()来设置

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

    在更新中,不能在set的列周围使用()

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

    虽然你已经正确地发现了问题,但我认为你应该告诉OP你改变了什么,否则他会冒着浪费时间去发现差异的风险。谢谢!我补充了一个解释。只是做了一点修改。如果插入,他还需要插入电话号码。当你正确地发现问题时,我认为你应该告诉OP你改变了什么,否则他会冒着浪费时间的风险去寻找差异。谢谢!我添加了一个解释。只是一个小小的修正。在插入的情况下,他还需要插入电话号码,这不能解决OP的问题。如果不存在电话号码,他们想插入一个新行。这不能解决OP的问题。他们想插入一个新的r如果一个不存在。