试图删除表中的所有内容

3D-*_*tiv -1 c# sql

我试图解决为什么我的代码无法正常工作.提示是预先准备的.我也想知道,当这个工作时,主键,在这种情况下,ID列也会重置并从1开始全部?

        connection = new SqlConnection(connectionString);
        connection.Open();
        sql = "DELETE * From Guests";
        sqlCommand = new SqlCommand(sql, connection);
        sqlCommand.EndExecuteNonQuery();
        connection.Close();
Run Code Online (Sandbox Code Playgroud)

pod*_*ska 5

你不需要星号

DELETE FROM Guests
Run Code Online (Sandbox Code Playgroud)

要重置主键,请使用

TRUNCATE TABLE Guests
Run Code Online (Sandbox Code Playgroud)

你想要的

sqlCommand.ExecuteNonQuery();  
Run Code Online (Sandbox Code Playgroud)

EndExecuteNonQuery

  • @ 3D-kreativ您必须使用`ExecuteNonQuery`而不是`EndExecuteNonQuery` (2认同)