如何检测MySql查询是否成功并输出消息

本文关键字:成功 是否 输出 消息 查询 MySql 何检测 检测 | 更新日期: 2023-09-27 18:07:06

我正在用c#编写一个图书馆管理系统。例如,我有一个添加书籍的表单。一旦您输入了所有的细节并运行查询将图书添加到数据库中,我想知道您如何检测查询是否成功并输出消息。

形式代码:

private void btnSubmit_Click(object sender, EventArgs e)
    {
        string isbn = txtISBN.Text;
        string title = txtbkTitle.Text;
        string author = txtbkAuthor.Text;
        string publisher = txtbkPublisher.Text;
        string imgPath = txtimgPath.Text;
        string catalog = txtCatalog.Text;

        book bc = new book(isbn, title, author, publisher, imgPath, catalog);
        utility = new DBUtlitiesBook();
        utility.Insert(bc);
    }

该代码获取用户条目的所有详细信息,将它们存储在变量中,并将它们发送给具有插入查询的a方法。

方法:

public override void Insert(object ob)
    {
        book bk = (book)ob;
        string isbn = bk.Isbn;
        string title = bk.Title;
        string author = bk.Author;
        string publisher = bk.Publisher;
        string localPath = bk.Imgpath;
        string catalogid = bk.CatalogID;

        MySqlConnection connection = new MySqlConnection(conn);
        MySqlCommand cmd;

        try
        {
            connection.Open();
            cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO BOOK(ISBN, title, author, publisher,imgPath, catalogID) VALUES ('" + isbn + "','" + title + "','" + author + "','" + publisher + "','" + localPath + "','" + catalogid + "')";
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (connection.State == System.Data.ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }

(此方法覆盖父类中的另一个方法)

你能帮我解决我的问题吗?

谢谢

如何检测MySql查询是否成功并输出消息

可以使用下面提到的代码

try
        {
            connection.Open();
            cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO BOOK(ISBN, title, author, publisher,imgPath, catalogID) VALUES ('" + isbn + "','" + title + "','" + author + "','" + publisher + "','" + localPath + "','" + catalogid + "')";
            int a= cmd.ExecuteNonQuery();
             if(a>0)
               //updated.
             else
              //Not updated.
        }
catch (Exception)
        {
           //Not updated.
        }

如果我理解正确的话,对于INSERT语句,ExecuteNonQuery返回受影响行的计数。这就是为什么你可以这样检查;

int rowCount = cmd.ExecuteNonQuery();
if(rowCount == 1) //or you can use > 0
{
   //Successful
}
else
{
  //Unsuccessfull
}
顺便说一下,请使用参数化查询。这种字符串连接容易受到SQL注入攻击。

也使用using语句来处理您的MySqlConnectionMySqlCommand

using(MySqlConnection connection = new MySqlConnection(conn))
using(MySqlCommand cmd = connection.CreateCommand())
{
    cmd.CommandText = "INSERT INTO BOOK(ISBN, title, author, publisher,imgPath, catalogID) VALUES (@isbn, @title, @author, @publisher, @path, @id)";
    ...
    //Add your parameter values one by one.
    connection.Open();
    int rowCount = cmd.ExecuteNonQuery();
    if(rowCount == 1) //or you can use > 0
    {
       //Successful
    }
    else
    {
      //Unsuccessfull
    }
}

ExecuteNonQuery返回指示受影响行数的整数。所以,如果什么都没做,它将返回0。您可以对其返回值设置布尔条件,然后返回消息或值本身。您可能需要修改父类,以便re可以有一个返回值。

错误由使用MySql连接器库时抛出的异常表示。如果你想处理失败,你不应该重新抛出异常。

您也不想捕获所有异常并立即处理它们。相反,只捕获MySqlException。我不确定类在哪个命名空间中,您必须自己找到它。

catch (MySqlException exception)
{
    // handle the error, show some message
}