0

Cシャープを介してストアドプロシージャを呼び出していますが、奇妙な理由で2回目の実行でタイムアウトします。

ストアド プロシージャを呼び出すコード:

  private void LoadData()
    {
        BackgroundWorker bw = new BackgroundWorker();

        bw.DoWork += new DoWorkEventHandler(bw_LoadData);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_LoadDataComplete);

        Busy.IsBusy = true;
        Busy.BusyContent = "Loading Data";
        bw.RunWorkerAsync();
    }
    void bw_LoadData(object sender, DoWorkEventArgs e)
    {

            SqlConnection con = new SqlConnection(Logic.GetConnectionString());
            con.Open();
            SqlCommand com = new SqlCommand("spGetUserData", con);
            com.CommandType = System.Data.CommandType.StoredProcedure;
            com.Parameters.Add(new SqlParameter("@UID", uid));




          //Timeouts here on the second run
     SqlDataReader readUserData = com.ExecuteReader();
            while (readUserData.Read())
            {

                    origname = readUserData[0].ToString();
                    origemail = readUserData[1].ToString();
                    origcontact = readUserData[2].ToString();
                    origadd1 = readUserData[3].ToString();
                    origadd2 = readUserData[4].ToString();
                    origstate = readUserData[5].ToString();
                    origcity = readUserData[6].ToString();
                    origzip = readUserData[7].ToString();

                    origcountry = readUserData[8].ToString();


            }
            con.Close();
            con.Dispose();
            e.Result = "OK";


    }
    void bw_LoadDataComplete(object sender, RunWorkerCompletedEventArgs e)
    {

        Busy.IsBusy = false;
        txtFullName.Text =  origname;
      txtEmail.Text  = origemail ;
         txtContact.Text= origcontact;
         txtAdd1.Text= origadd1;
       txtAdd2.Text=  origadd2 ;
        txtState.Text=  origstate;
         txtCity.Text= origcity;
       txtZip.Text =  origzip;
       cboCountry.SelectedItem = origcountry;

    }

ウィンドウロードイベント中の最初のメソッド呼び出し..期待どおりに動作します。

       private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        LoadData();

    }

タイムアウトが発生した 2 番目のメソッド呼び出し。

    void bw_ChangeEmailComplete(object sender, RunWorkerCompletedEventArgs e)
     {
        if (e.Result.ToString() == "OK")
        {
            Busy.IsBusy = false;
          MessageBox.Show("The Email Address was changed successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);


        }
        else
        {
            Busy.IsBusy = false;
            MessageBox.Show("An Unexpected Error occured or email already exist", "Error", MessageBoxButton.OK, MessageBoxImage.Error);



        }
        LoadData();


    }

そして最後にストアドプロシージャ

          Create Proc [dbo].[spGetUserData]
         @UID varchar(50)
         AS
         Select FullName,Email,Contact,Address1,Address2,State,City,Zip,Country,SubDate,SID
         FROM Users
         Where UID = @UID

アップデート

これを試してもまだ動作せず、データリーダーを手動で破棄する

          using (SqlConnection con = new SqlConnection(Logic.GetConnectionString()))
            {

                using (SqlCommand com = new SqlCommand("spGetUserData", con))
                {
                    com.CommandType = System.Data.CommandType.StoredProcedure;
                    com.Parameters.Add(new SqlParameter("@UID", uid));



                    con.Open();
                    using (var readUserData = com.ExecuteReader())
                    {
                        while (readUserData.Read())
                        {

                                origname = readUserData[0].ToString();
                                origemail = readUserData[1].ToString();
                                origcontact = readUserData[2].ToString();
                                origadd1 = readUserData[3].ToString();
                                origadd2 = readUserData[4].ToString();
                                origstate = readUserData[5].ToString();
                                origcity = readUserData[6].ToString();
                                origzip = readUserData[7].ToString();

                                origcountry = readUserData[8].ToString();


                        }

                    }
                }
            }
4

2 に答える 2

1

DataReader を破棄するのを忘れましたreadUserData

それをusingステートメントに入れます:

using (var readUserData = com.ExecuteReader())
{
    while (readUserData.Read())
        ...
}

( usingSqlConnection にも使用します。手動でand/orusingを呼び出すよりも厳密に優れています。)Close()Dispose()

于 2013-02-09T16:01:48.700 に答える
0

接続、コマンド、リーダーなどが自動的に閉じられるか破棄されるようにコードを構成します。以下の例を参照してください。

using (SqlConnection conn = new SqlConnection(--Conn String Here--)
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        // Set Conn Properties Here
        Conn.Open();
        using (SqlDataReader rdr = cmd.ExecuteReader())
        {

        } // Reader automatically disposed here
    }
} // Conn automatically closed here
于 2013-02-09T16:09:48.633 に答える