2017-05-18 6 views
0

コードは次のとおりです。 商品:データアクセス問題

/// <summary> 
    /// Method to insert an Commodity 
    /// </summary> 
    /// <param name="commodity">commodity's information</param> 
    /// <returns>void</returns> 
    public bool Add(Models.CommodityInfo commodity) 
    { 
     if (string.IsNullOrEmpty(commodity.Sp_itemnum) || string.IsNullOrEmpty(commodity.Sp_name)) 
      return false; 
     else 
      return dal.Insert(commodity); 
    } 
    ///interface is omitted 
    /// <summary> 
    /// Method to insert an Commodity 
    /// </summary> 
    /// <param name="commodity">commodity's information</param> 
    /// <returns>Whether the operation is successful</returns> 
    public bool Insert(CommodityInfo commodity) 
    { 
     StringBuilder strSQL = new StringBuilder(SQL_INSERT_COMMODITY); 

     // Get each commands parameter arrays 
     SqlParameter[] Parms = GetParameters(); 

     SqlCommand cmd = new SqlCommand(); 

     // Set up the parameters 
     Parms[0].Value = commodity.Sp_itemnum; 
     Parms[1].Value = commodity.Sp_name; 
     Parms[2].Value = commodity.Sp_spec; 
     Parms[3].Value = commodity.Sp_classify; 
     Parms[4].Value = commodity.Sp_brand; 
     Parms[5].Value = commodity.Sp_purcprice; 
     Parms[6].Value = commodity.Sp_saleprice; 
     Parms[7].Value = commodity.Sp_origin; 
     Parms[8].Value = commodity.Sp_pack; 
     Parms[9].Value = commodity.Sp_note; 
     foreach (SqlParameter parm in Parms) 
      cmd.Parameters.Add(parm); 

     // Create the connection to the database 
     using (SqlConnection conn = new SqlConnection(SqlHelper.connStr)) 
     { 
      conn.Open(); 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = strSQL.ToString(); 

      int r = cmd.ExecuteNonQuery(); 
      //Clear the parameters 
      cmd.Parameters.Clear(); 
      return Convert.ToBoolean(r); 
     } 
    } 

ストレージ:

/// <summary> 
    /// Method to add a commodity 
    /// </summary> 
    /// <param name="spName">name of commodity of add</param> 
    /// <returns>Whether the operation is successful</returns> 
    public bool addCommodity(string spName) 
    { 
     bool r = true; 

     BLL.WareHouseManager bwm = new WareHouseManager(); 
     BLL.CommodityManager bcm = new CommodityManager(); 
     Models.StorageInfo msi = new Models.StorageInfo(); 
     msi.Sp_id = bcm.GetCommodityIdbyName(spName); 
     msi.Cc_num = 0; 

     IList < int> ckIds = bwm.GetAllCkId(); 
     for(int i = 0; i < ckIds.Count; ++i) 
     { 
      msi.Ck_id = ckIds[i]; 
      if (!dal.Insert(msi)) 
       r = false; 
     } 
     return r; 
    } 
    /// <summary> 
    /// Method to Insert a storageinfo 
    /// </summary> 
    public bool Insert(Models.StorageInfo msi) 
    { 
     StringBuilder strSQL = new StringBuilder(SQL_INSERT_RECORD); 

     // Get each commands parameter arrays 
     SqlParameter[] parms = GetUpdateParameters(); 

     SqlCommand cmd = new SqlCommand(); 

     // Set up the parameters 
     parms[0].Value = msi.Ck_id; 
     parms[1].Value = msi.Sp_id; 
     parms[2].Value = msi.Cc_num; 

     foreach (SqlParameter parm in parms) 
      cmd.Parameters.Add(parm); 

     // Create the connection to the database 
     using (SqlConnection conn = new SqlConnection(SqlHelper.connStr)) 
     { 
      conn.Open(); 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = strSQL.ToString(); 

      int r = cmd.ExecuteNonQuery(); 
      //Clear the parameters 
      cmd.Parameters.Clear(); 
      return Convert.ToBoolean(r); 
     } 
    } 

質問です: データベースのテーブル構造: Table of commodity and storage

エラーの場所:

  if(addCommodity.Add(addCommodityInfo))//add a commodity into table of commodity 
     { 
      if (bsm.addCommodity(addCommodityInfo.Sp_name))//set the number of stores in every warehouse to 0---------》error point(In debug,the error is when its SQL was executing) 
      { 
       MessageBox.Show("operation success", "Tips", MessageBoxButtons.OK); 
       table_SPTableAdapter.Fill(jcxWorksDataSet.Table_SP); 
      } 
      else 
      { 
       addCommodity.Delete(addCommodityInfo.Sp_name); 
      } 
     } 

VS2015与えられた情報「外部キーの競合」、しかしそれは、SQLサーバーのManagermentスタジオ

その他に正常に実行されます:私は中国人です。あなたは中国の知っている場合は、この enter link description here

+1

ではありませんTable_CC.sp_id列にいくつかの値を挿入しようとしている場合にはチェック探すことができます。.. – BugFinder

+0

返信いただきありがとうございます。問題は修正されています。 – wuyi

答えて

0

はちょうどあなたが、あなたが実際のエラーが何であるかを概説することができますしてくださいTable_SP.sp_id

+0

ご返信ありがとうございます。これは私の質問のキーです。私がTable_CC.sp_idに値を挿入しようとする前に、私はTable_SP.sp_idに必要な値を挿入します。私はデバッグで取得したデータを使用しました。これはSQL Serverのマネージャースタジオ – wuyi

+0

が 'dbo.Table_CK'のところで正常に実行されています。 @ wuyi – Abi

+0

申し訳ありません。私の不注意により間違ったイメージが表示されました。スクリーンショットのエラーは、同様のコード構造の別のエラーです。すべてが商品保管倉庫です。 dbo.Table_CKは倉庫のテーブルです。 – wuyi

関連する問題