C# C第二次运行时存储过程超时

C# C第二次运行时存储过程超时,c#,sql-server,wpf,sql-server-2012,C#,Sql Server,Wpf,Sql Server 2012,我通过C sharp调用一个存储过程,由于某种奇怪的原因,它在第二次运行时超时 调用存储过程的代码: private void LoadData() { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler(bw_LoadData); bw.RunWorkerCompleted += new RunWorkerComplete

我通过C sharp调用一个存储过程,由于某种奇怪的原因,它在第二次运行时超时

调用存储过程的代码:

  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();

    }
发生超时的第二个方法调用

    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();


                        }

                    }
                }
            }

您忘记处理DataReader,
readUserData

使用语句将其放入

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

(对您的SqlConnection也使用
using
使用
比手动调用
Close()
和/或
Dispose()
要好得多)

构造您的代码,以便您的连接、命令、读卡器等。。。自动关闭或处理。见下面的例子

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

哎哟您正在将DataReader传递给另一个线程。那不好。删除
Invoke
,或将数据复制到自定义对象,并将自定义对象传递给
Invoke
。我完全删除了Invoke,它在单线程中工作,但仍然超时。再次更新问题以显示我所做的。后台工作人员不应访问GUI元素,请尝试第二种方法(使用POCO对象调用)我完全删除了所有内容,只是让datareader执行,而不是从中读取数据..仍然超时:/感谢您尝试帮助我..显然,我了解到,当您不回滚异常事务时,会发生不好的事情。