0

同様のクエリを使用していますが、デバッグ時に影響を受ける行が-1になるのに対し、SQLサーバーで同じクエリを実行すると、影響を受ける行が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

Try the following:

Instead of using:

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

use:

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

Maybe it's a problem with the type of the field. You have to replace the DbType.Int in the example trough the correct type of the database-field.

于 2012-04-19T09:19:30.987 に答える