如何检测MySql查询是否成功并输出消息? [英] How to detect if a MySql query was successful or not and output a message?

查看:52
本文介绍了如何检测MySql查询是否成功并输出消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 制作图书馆管理系统.例如,我有一个表格来添加一本书.输入所有详细信息并运行查询以将书籍添加到数据库后,我想知道您如何检测查询是否成功并输出消息.

I am making a library management system using C#. For example I have a form to add a book. Once you input all the details and run the query to add books to the database, I wish to know how you can detect if the query was successful or not and output a message.

表格代码:

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 方法.

This code takes all the details of user entries, store them in variables and send them to the a method which have the insert query.

这是方法:

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();

            }
        }
    }

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

(This method overrides another method which is in the parent class)

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

Can you help me solve my problem?

谢谢

推荐答案

你可以使用下面提到的代码

you can use below menioned code

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.
        }

这篇关于如何检测MySql查询是否成功并输出消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆