在executereader中受影响的行-1 - 类似于查询

时间:2012-04-19 08:26:43

标签: asp.net c#-4.0

我正在使用类似的查询,在调试时给我受影响的行为-1,而当我在sql server中运行相同的查询时返回1个受影响的行

 public ResultClass fn_GetUserMasterSearch(UserMasterClass objusmspass)
    {
        UserMasterClass objusms = null;
        eslist<UserMasterClass> objusList = null;

        try
        {
            objConnection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            objConnection.Open();
            StringBuilder strquery = new StringBuilder();
            StringBuilder strWhere = new StringBuilder();
            objCommand = new SqlCommand();
            strquery.Append("select user_id,user_name,user_type,default_company_code,created_by,created_on,updated_on from user_master where");

            if (objusmspass.struser_name != "")
            {
                strWhere.Append(" user_name like @user_name ");
                objCommand.Parameters.AddWithValue("@user_name", objusmspass.struser_name);
            }
            if (objusmspass.struser_type != "")
            {
                if (strWhere.ToString() != null)
                    strWhere.Append(" and ");
                strWhere.Append(" user_type like @user_type ");
                objCommand.Parameters.AddWithValue("@user_type", objusmspass.struser_type);
            }
            if (objusmspass.strcompany_code != "")
            {
                if (strWhere.ToString() != null)
                    strWhere.Append(" and ");
                strWhere.Append(" company_code like @company_code ");
                objCommand.Parameters.AddWithValue("@company_code", objusmspass.strcompany_code);
            }

            if (objusmspass.icreated_by != 0)
            {
                if (strWhere.ToString() != null)
                    strWhere.Append(" and ");
                strWhere.Append(" created_by like @created_by ");
                objCommand.Parameters.AddWithValue("@created_by", objusmspass.icreated_by);
            }
            if (objusmspass.dtcreated_on != null)
            {
                if (strWhere.ToString() != null)
                    strWhere.Append(" and ");
                strWhere.Append(" created_on like @created_on ");
                objCommand.Parameters.AddWithValue("@created_on", objusmspass.dtcreated_on);
            }
            if (objusmspass.dtupdated_on != null)
            {
                if (strWhere.ToString() != null)
                    strWhere.Append(" and ");
                strWhere.Append(" updated_on like @updated_on ");
                objCommand.Parameters.AddWithValue("@updated_on", objusmspass.dtupdated_on);
            }
            objCommand.CommandText = strquery.ToString() + strWhere.ToString();
            objCommand.Connection = objConnection;           
            objReader = objCommand.ExecuteReader();

            objusList = new eslist<UserMasterClass>();

            while(objReader.Read())
            {
                objusms = new UserMasterClass();
                objusms.iuser_id = Convert.ToInt32(objReader["user_id"].ToString().Trim());
                objusms.struser_name = objReader["user_name"].ToString().Trim();
                objusms.struser_type = objReader["user_type"].ToString().Trim();
                objusms.strcompany_code = objReader["default_company_code"].ToString().Trim();
                objusms.icreated_by = Convert.ToInt32(objReader["created_by"].ToString().Trim());
                objusms.dtcreated_on = objReader["created_on"]==DBNull.Value?DateTime.Now:Convert.ToDateTime(objReader["created_on"].ToString().Trim());
                objusms.dtupdated_on = objReader["updated_on"]==DBNull.Value?DateTime.Now:Convert.ToDateTime(objReader["updated_on"].ToString().Trim());
                objusList.Add(objusms);

            }

            return new ResultClass(true, "User Exist", objusList, int.Parse(objReader["user_id"].ToString()), null);
        }

我收到了这一行的错误

while(objReader.Read())

调试结果显示受影响的行为-1。

谢谢,

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

而不是使用:

objCommand.Parameters.AddWithValue("@user_name", objusmspass.struser_name);

使用:

objCommand.Parameters.Add(new SqlParameter("@user_name", DbType.Int){ Value = objusmspass.struser_name}));

可能这是该字段类型的问题。您必须通过正确的数据库字段类型替换示例中的DbType.Int。