2017-03-20 16 views
1

foreachループを使用してレコードを挿入するストアドプロシージャを適切に実行するのが最適です。ループ内で挿入ストアドプロシージャを実行するベスト/適切な方法C#

これまで私がこれまで持っていたことは次のとおりです。このようにそれをやって

string constr = ConfigurationManager.ConnectionStrings["CurrencyDb"].ConnectionString; 

    using (SqlConnection con = new SqlConnection(constr)) { 
    con.Open(); 
     foreach (ListItem i in DependenciesListBox.Items) { 
      if (i.Selected) { 
       using (SqlCommand cmd = new SqlCommand("dbo.InsertDependency", con)) { 
        try { 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("@CurrencyId", scopeidentity); 
         cmd.Parameters.AddWithValue("@DependencyId", i.Value); 
         cmd.ExecuteNonQuery(); 
        } 
        catch (SqlException sqlex) { 
         throw new Exception("SQL Exception on insert. " + sqlex.Message); 
        } 
        catch (Exception ex) { 
         throw new Exception("Error adding dependencies. " + ex.Message); 
        } 
       } 
      } 
     } 

     foreach (ListItem i in AffectedListBox.Items) { 
      if (i.Selected) { 
       using (SqlCommand cmd = new SqlCommand("dbo.InsertAffected", con)) { 
        try { 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("@DependencyId", scopeidentity); 
         cmd.Parameters.AddWithValue("@CurrencyId", i.Value); 
         cmd.ExecuteNonQuery(); 
        } 
        catch (SqlException sqlex) { 
         throw new Exception("SQL Exception on insert. " + sqlex.Message); 
        } 
        catch (Exception ex) { 
         throw new Exception("Error adding affected apps. " + ex.Message); 
        } 
       } 
      } 
     } 

     //Loops through Platform list box and for each item that's selected, add a record into the platform table in the database. 
     foreach (ListItem i in PlatformListBox.Items) { 
      if (i.Selected) { 
       using (SqlCommand cmd = new SqlCommand("dbo.InsertPlatform", con)) { 
        try { 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("@CurrencyId", scopeidentity); 
         cmd.Parameters.AddWithValue("@PlatformId", i.Value); 
         cmd.ExecuteNonQuery(); 
        } 
        catch (SqlException sqlex) { 
         throw new Exception("SQL Exception on insert. " + sqlex.Message); 
        } 
        catch (Exception ex) { 
         throw new Exception("Error adding platforms. " + ex.Message); 
        } 
       } 
      } 
     } 
    } 

、私は誰も私にはベストプラクティスは、この種のものをやって何のためにあるのか知っているようすることができるだろう、次のエラー(更新)

Uncaught Error: Sys.WebForms.PageRequestManagerServerErrorException: Error adding dependencies. The connection was not closed. The connection's current state is open. 
    at Function.Error.create (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at Sys.WebForms.PageRequestManager._createPageRequestManagerServerError (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at Sys.WebForms.PageRequestManager._parseDelta (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at Sys.WebForms.PageRequestManager._onFormSubmitCompleted (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at Array.<anonymous> (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1 
    at Sys.Net.WebRequest.completed (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 
    at XMLHttpRequest._onReadyStateChange (MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81:1) 

を取得しますか?事前にありがとうございます。

+0

例外表の最初の行を参照してください

using (SqlConnection con = new SqlConnection(constr)) { con.Open(); ... 
に移動します。これは、読みやすく、検索するのを容易にします。 – mason

+0

あなたは正しいです、私の謝罪。 – David

答えて

1

con.Open();に複数回電話をかけないでください。テキストではなく画像としてあなたのエラーメッセージを追加してください

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=vs.110).aspx

+0

私はそれが何かばかげていることを知っていました。これを試してみましょう、ありがとう。 – David

+0

うーん、私はまだ同じエラーに遭遇します。 – David

+0

@David - 3つの異なるループに 'con.Open();'があるので、それらをすべて削除して、表示されているものだけを残してください。 – Igor

関連する問題