如何高效地调试代码 [英] How to debug code effieciently

查看:77
本文介绍了如何高效地调试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试最后一行代码的return语句。我当前正在接收抛出的System.FormatExceptionError。我被告知,目前的返回声明正试图完成3件事。为了知道抛出错误的位置,我相信我需要在单独的return语句中执行这3个操作。以下是代码:

I'm currently trying to debug the return statement for my last line of code. I'm currently receiving a System.FormatExceptionError thrown there. I've been told that the return statement, as is currently, is attempting to accomplish 3 things. In order to know where the error is being thrown, I believe I need to execute those 3 things in separate return statements. Here's the code:

public int UpdateConnectionType(int connectionTypeID, string lastUpdatedBy, string connectionTypeDesc, bool isDisabled )
        {
            var sqlStatement = new StringBuilder();

            sqlStatement.Append(" UPDATE dbo.ConnectionType");
            sqlStatement.Append("   SET");
            sqlStatement.Append(" LastUpdatedBy = @LastUpdatedBy, ");
            sqlStatement.Append(" ConnectionTypeDesc = @ConnectionTypeDesc,");
            sqlStatement.Append(" IsDisabled = @IsDisabled, ");
            sqlStatement.Append(" LastUpdatedDate = GetDate() ");
            sqlStatement.Append(" WHERE ");
            sqlStatement.Append("   ConnectionTypeID = @ConnectionTypeID");
            sqlStatement.Append(" SELECT @@Identity");

            SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString());
            var sqlParams = new List<SqlParameter>();

            sqlParams.Add(new SqlParameter() { ParameterName = "@ConnectionTypeID", SqlDbType = SqlDbType.Int, Value = connectionTypeID });
            sqlParams.Add(new SqlParameter(){ParameterName = "@IsDisabled", SqlDbType = SqlDbType.Bit, Value = isDisabled});
            sqlParams.Add(new SqlParameter() {ParameterName = "@LastUpdatedBy", SqlDbType = SqlDbType.VarChar, Size = 200, Value = lastUpdatedBy });
            sqlParams.Add(new SqlParameter() { ParameterName = "@ConnectionTypeDesc", SqlDbType = SqlDbType.VarChar, Size = 100, Value = connectionTypeDesc});

            sqlCommand.Parameters.AddRange(sqlParams.ToArray());

            //int result = DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("AccountTracker"), sqlCommand);
            return DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("AccountTrackerDB"), sqlCommand);
        }



我的问题是:

1)我的回复声明试图做什么3件事完成? (我相信它正在尝试获取ID,然后更新数据库中的相应记录)

2)如何将这3个任务写入自己的return语句? (我已经用注释掉的代码对它进行了一次尝试)谢谢!


My questions are:
1) What 3 things is my return statement trying to accomplish? (I believe it's attempting to get the ID, and then update the corresponding record within the database)
2)How would I write those 3 tasks into their own return statement? (I've taken one stab at it with the code that commented out) Thanks!

推荐答案

我看不到三个而是两个可以从返回中分离出来的动作声明:

I cannot see three but rather two actions that could be separated from the return statement:
string connectionString = DBAccess.SQLServer.GetConnectionString("AccountTrackerDB");
int result = DBAccess.SQLServer.GetInteger(connectionString, sqlCommand);
return result;



通过这种方式,您可以检查变量的内容在使用和返回之前。


This way you will have the ability to check the content of the variables before they are used and returned.


这篇关于如何高效地调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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