简体   繁体   English

在VB.NET中删除SQL语句

[英]Delete SQL statement in VB.NET

I am new to VB.NET and as well as MySqL. 我是VB.NET和MySqL的新手。 I want to delete records in my database. 我想删除数据库中的记录。 Using, 使用,

    Dim SQLStatement As String = "DELETE name,date FROM people"
    RetrieveInfos(SQLStatement)

Where, 哪里,

Public Sub RetrieveInfos(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
        .Connection = SQLConnection
        .ExecuteNonQuery()

    End With

    SQLConnection.Close()
    SQLConnection.Dispose()
End Sub

But there is an error of InvalidOperationException, I am confused. 但是有一个InvalidOperationException错误,我很困惑。 I want to delete records in my database without referencing the " WHERE number = VALUE " in order to delete the previous recorded value. 我想删除数据库中的记录而不引用“ WHERE number = VALUE”,以便删除以前的记录值。 Is that possible? 那可能吗?

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

You've got a few problems: 您遇到了一些问题:

  1. You do not specify column names in the delete statement since you are deleting records. 由于要删除记录,因此您无需在delete语句中指定列名。

  2. Your command, should it be successfully executed, will delete every record in the people table. 您的命令(如果成功执行)将删除人员表中的每条记录。

  3. You are closing and disposing the SQLConnection at the end of the RetrieveInfos method. 您将在RetrieveInfos方法的末尾关闭并处理SQLConnection。 If this connection is open elsewhere in your code, only the first execution of the code will work. 如果此连接在您代码的其他位置打开,则仅代码的第一次执行将起作用。

  4. Your method should not be called Retrieve if it is deleting. 如果您的方法正在删除,则不应将其称为“检索”。

  5. The command should be wrapped in a using statement 该命令应包装在using语句中

     Using cmd As MySqlCommand = New MySqlCommand With cmd .CommandText = SQLStatement .CommandType = CommandType.Text .Connection = SQLConnection .ExecuteNonQuery() End With End Using 

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

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