C#如何捕获错误并调试准备好的语句

本文关键字:准备好 语句 调试 何捕获 错误 | 更新日期: 2023-09-27 18:25:44

嗨,下面是我的带有多插入语句的C#代码片段。我把它们放在一个事务括号中,并使用预先准备好的语句。例如,我的挑战是,我知道我现在在cmdText3中遇到了问题,但没有生成任何错误消息。在这种情况下,我应该如何添加额外的try-and-catch来帮助我调试并提醒我错误?在准备好的语句中,我能看到用数据执行的确切查询是什么吗?

myCon getMyCon1=新建myCon();MySqlConnection con=getMyCon1.GetConnection();

    using (MySqlTransaction trans = con.BeginTransaction())
    {
        try
        {
            long rID = 0;
            String cmdText1 = "INSERT INTO record1(fiscalPeriod,financialYear) VALUES(@fiscalPeriod,@financialYear)";
            using (MySqlCommand cmd = new MySqlCommand(cmdText1, con, trans))
            {
                cmd.Parameters.AddWithValue("@fiscalPeriod", fiscalPeriod.SelectedValue);
                cmd.Parameters.AddWithValue("@financialYear", financialYear.SelectedValue);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                rID = cmd.LastInsertedId;
            }
            foreach (object line in linesC)
            {
                if (line.GetType() == typeof(TypeC))
                {
                    TypeC typesC = (TypeC)line;                           
                    String companyName = typesC.comName;                            
                    String periodStart = typesC.periodStart;
                    String periodEnd = typesC.periodEnd;                          
                    String cmdText2 = "INSERT INTO record2(rID,companyName,,periodStart,periodEnd,) VALUES(@rID,@companyName,@periodStart,@periodEnd)";
                    using (MySqlCommand cmd = new MySqlCommand(cmdText2, con, trans))
                    {
                        cmd.Parameters.AddWithValue("@rID", rID);
                        cmd.Parameters.AddWithValue("@companyName", companyName);                                
                        cmd.Parameters.AddWithValue("@periodStart", periodStart);
                        cmd.Parameters.AddWithValue("@periodEnd", periodEnd);                               
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                }
            }
            foreach (object line in linesP)
            {
                if (line.GetType() == typeof(TypeP))
                {
                    TypeP typesP = (TypeP)line;
                    String supplierName = typesP.supName;                          
                    String invoiceDate = typesP.invoiceDate;
                    String invoiceNo = typesP.invoiceNo;
                    String cmdText3 = "INSERT INTO record2(rID,supplierName,invoiceDate,invoiceNo) VALUES(@rID,@supplierName,@invoiceDate,@invoiceNo,)";
                    using (MySqlCommand cmd = new MySqlCommand(cmdText3, con, trans))
                    {
                        cmd.Parameters.AddWithValue("@rID", rID);
                        cmd.Parameters.AddWithValue("@supplierName", supplierName);
                        cmd.Parameters.AddWithValue("@invoiceDate", invoiceDate);
                        cmd.Parameters.AddWithValue("@invoiceNo", invoiceNo);
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                }
            }
            trans.Commit();                    
        }
        catch (Exception ex)
        {
            trans.Rollback();
        }
    }
    con.Close()

C#如何捕获错误并调试准备好的语句

您可以使用Sql Profiler应用程序查看确切的查询。这将允许您查看生成的查询和来自Sql Server的响应。

https://msdn.microsoft.com/en-us/library/ms181091.aspx

以下内容:

String cmdText3 = "INSERT INTO record2(rID,supplierName,invoiceDate,invoiceNo) VALUES(@gafID,@supplierName,@invoiceDate,@invoiceNo,)";

与您在此处指定的第一个参数不一致:

cmd.Parameters.AddWithValue("@rID", rID);
cmd.Parameters.AddWithValue("@supplierName", supplierName);
cmd.Parameters.AddWithValue("@invoiceDate", invoiceDate);
cmd.Parameters.AddWithValue("@invoiceNo", invoiceNo);

替换:

VALUES(@gafID,@supplierName,@invoiceDate,@invoiceNo,)

带有:

VALUES(@rID,@supplierName,@invoiceDate,@invoiceNo)

或者,您可以重新命名:

cmd.Parameters.AddWithValue("@rID", rID);

收件人:

cmd.Parameters.AddWithValue("@gafID", rID);