根据输入检索行

发布于 2024-08-27 06:15:51 字数 321 浏览 1 评论 0原文

public void RemoveTask(int index) {
  SQL = "DELETE FROM Task where (...) = " +index;

  dbConn.Open();

  dbCommand = new SqlCeCommand(SQL, dbConn);
  dbCommand.ExecuteNonQuery();

  dbConn.Close();
}

我想要做的是根据指定行号的索引删除记录,但我不知道应该使用什么函数或变量(注意空白),我尝试了类似 rowNum 的方法,但它不起作用。

任何帮助将不胜感激

public void RemoveTask(int index) {
  SQL = "DELETE FROM Task where (...) = " +index;

  dbConn.Open();

  dbCommand = new SqlCeCommand(SQL, dbConn);
  dbCommand.ExecuteNonQuery();

  dbConn.Close();
}

What i want to do is to delete the record based on the index which specified the row number but I don't know what function or variable should be used ( note the blank ), i try something like rowNum but it does not work.

any help will be appreaciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-09-03 06:15:51

目前尚不完全清楚您要做什么。我认为以下代码就是您想要的 - 它根据主键删除一行,在这种情况下,主键列的名称是 TaskId (但您可以根据表列名称更改它)。

请注意,它还使用参数化 SQL,从而提供更好的性能和安全性。

SQL = "DELETE FROM Task where TaskId = @taskid"; 

dbConn.Open(); 

dbCommand = new SqlCeCommand(SQL, dbConn); 

dbCommand.Parameters.Add("@taskid", SqlDbType.Int);
dbCommand.Parameters["@taskid"].Value = index;

dbCommand.ExecuteNonQuery(); 

dbConn.Close(); 

It isn't entirely clear what you are trying to do. I think the following code is what you are after - it deletes a row based on the primary key where in this case the name of the primary key column is TaskId (but you can change that based on your table column names).

Note that it also uses parameterised SQL which gives better performance and security.

SQL = "DELETE FROM Task where TaskId = @taskid"; 

dbConn.Open(); 

dbCommand = new SqlCeCommand(SQL, dbConn); 

dbCommand.Parameters.Add("@taskid", SqlDbType.Int);
dbCommand.Parameters["@taskid"].Value = index;

dbCommand.ExecuteNonQuery(); 

dbConn.Close(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文