亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從MessageBox中的數據庫中檢索信息| IndexOutOfBoundsException

從MessageBox中的數據庫中檢索信息| IndexOutOfBoundsException

C#
一只甜甜圈 2021-05-12 17:54:56
我有一個GetProduct方法,該方法應該在MessageBox中返回產品代碼,描述和價格。當前,當它實際上找到與代碼匹配的產品時,我只能在“ IndexOutOfBoundsException”框上顯示帶有標題的單詞“ Price” 。如果沒有,則顯示找不到。這是代碼:        public static Product GetProduct(string code)    {        SqlConnection connection = Connection.GetConnection();        string select = @"SELECT ProductCode, Description, UnitPrice FROM Products WHERE ProductCode = @ProductCode";        SqlCommand selectCommand = new SqlCommand(select, connection);        SqlParameter pCode = new SqlParameter();        pCode.ParameterName = "@ProductCode";        pCode.Value = product.Code;        SqlParameter pDesc = new SqlParameter();        pDesc.ParameterName = "@Description";        pDesc.Value = product.Description;        SqlParameter pPrice = new SqlParameter();        pPrice.ParameterName = "@UnitPrice";        pPrice.Value = product.Price;        selectCommand.Parameters.AddWithValue("@ProductCode", code);        try        {            connection.Open();            SqlDataReader prodReader = selectCommand.ExecuteReader(CommandBehavior.SingleRow);            if (prodReader.Read())            {                product.Code = prodReader["ProductCode"].ToString(); ;                product.Description = prodReader["Description"].ToString();                product.Price = ((decimal)prodReader["Price"]);                return product;            }            else            {                return null;            }        }        catch (SqlException ex)        {            throw ex;        }        finally        {            connection.Close();        }    }
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號