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。

谢谢,

4

1 回答 1

0

尝试以下操作:

而不是使用:

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

利用:

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

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

于 2012-04-19T09:19:30.987 回答