简体   繁体   English

数据库删除行

[英]Database Deleting Rows

so this is my code 所以这是我的代码

for (int i = 0; i < users_table.RowCount; ++i)
{
    SQLDatabase.DatabaseRow row = users_table.GetRow(i); //get current ID.
    if (userID == users_table.GetRow(i)["ID"]) //if userID is the same as it's in the row
    {
        row["ID"] = "0";
        row["Name"] = "DELETED";
        row["Username"] = "DELETED";
        row["Password"] = "DELETED";
        row["UserType"] = "DELETED";
        row["LastLoginDate"] = "0";
        row["LastLoginTime"] = "0";
        users_table.Update(row);
    }
}

My goal is to delete a row. 我的目标是删除一行。 Sadly I'm unable to insert NULL values etc., any idea how I could delete that row? 可悲的是,我无法插入NULL值等,知道如何删除该行吗? I would like to add that I do not have .delete functions or anything in particular. 我想补充一点,我没有.delete函数或任何特别的东西。 I would be glad if you could tell me how to add them. 如果您能告诉我如何添加它们,我将非常高兴。

Thanks. 谢谢。

(This answer assumes you're using SQL Server) (此答案假设您使用的是SQL Server)

You can easily execute SQL Commands using System.Data.SqlClient. 您可以使用System.Data.SqlClient轻松执行SQL命令。 The connection string would be the connection details to your database instance. 连接字符串将是您数据库实例的连接详细信息。 You can see some more examples of how to construct your connection string @ Connection String Examples . 您可以看到更多有关如何构造连接字符串@Connection String Examples的示例 Also, when you're deleting rows from your database you want to utilize the DELETE command not the UPDATE. 另外,当您从数据库中删除行时,您想使用DELETE命令而不是UPDATE。 More details can be found on DELETE (Transact-SQL) 可以在DELETE(Transact-SQL)上找到更多详细信息。

    public void deleteRow()
    {
       string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password = myPassword; ";
       string queryString = "DELETE FROM Test_Table WHERE ID = 5";
       using (SqlConnection connection = new SqlConnection(
       connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM