C#如何捕获错误并调试预处理语句?

时间:2015-12-04 16:39:04

标签: c# mysql prepared-statement

以下是我的C#代码片段,其中包含多个insert语句。我将它们放入事务括号中,并使用预处理语句。我的挑战,例如我知道我现在在cmdText3中遇到了问题,但没有生成任何错误消息。在这种情况下,我应该在哪里添加额外的try和catch来帮助我调试并提醒我错误?可以在准备好的语句中看到用数据执行的确切查询是什么?

myCon getMyCon1 = new 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()

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

以下内容:

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