SQLException超时

时间:2017-10-05 13:53:04

标签: c# sql-server

代码在下面,问题似乎是一个让我耗费大量时间的笑话。调用此方法我发出一个请求,并在字段PIVA为空的情况下找到或多或少的4000条记录。所以我调用一个函数,用函数agm.getPIVA()查询一个站点,有时在那里找到一个PIVA。所以我想更新数据库但是有问题,更新请求会导致SQLException超时。如您所见,操作非常短,仅涉及一(1)条记录。 其他事情? IDAZIENDA是主键,总表有大约30.000行。 一个嫌疑人是因为某些原因我不能以这种方式使用相同的连接,但我复制了一个有效的旧功能,看起来是一回事。连接字符串启用MARS,所以我有点困惑,理论上问题不应该出现。

任何建议都会受到欢迎。提前谢谢。

private void btnUpdateGuideM_Click(object sender, EventArgs e)
{
String selectSQL = "SELECT "
        + " AGAZIENDE.RAGSOC,"
        + " AGAZIENDE.IDAZIENDA,"
        + " FROM"
        + " AGAZIENDE,"
        + " WHERE "
        + " AGAZIENDE.IDAZIENDA>@STARTBY"
        + " AND LEN(AGAZIENDE.PIVA)<>11"
        + " AND AGAZIENDE.VALID = 1"
        + " ORDER BY AGAZIENDE.IDAZIENDA";

string updateSQL = "UPDATE AGAZIENDE "
        + "SET PIVA=@PIVA "
        + "WHERE IDAZIENDA=@IDAZIENDA";

using (SqlConnection awConnection = new SqlConnection(connectionString))
{
    SqlCommand noWebLeadsCmd = new SqlCommand(selectSQL, awConnection);
    noWebLeadsCmd.Parameters.Add("@STARTBY", SqlDbType.Int);
    noWebLeadsCmd.Parameters["@STARTBY"].Value =Int32.Parse(tbStartBy.Text);

        SqlCommand updateCmd = new SqlCommand(updateSQL, awConnection);
        updateCmd.Parameters.Add("@IDAZIENDA", SqlDbType.Int);
        updateCmd.Parameters.Add("@PIVA", SqlDbType.Text);

        awConnection.Open();
            try
        {
            using (SqlDataReader selectDR = noWebLeadsCmd.ExecuteReader())
            {
                try
                {
                    while (selectDR.Read())
                    {
                        string  codNazione = selectDR.GetFieldValue<string>(0);
                        string  nomeNazione = selectDR.GetFieldValue<string>(1);
                        string  codRegione = selectDR.GetFieldValue<string>(2);
                        string  nomeRegione = selectDR.GetFieldValue<string>(3);
                        string  codProvincia = selectDR.GetFieldValue<string>(4);
                        string  nomeProvincia = selectDR.GetFieldValue<string>(5);
                        string  codComune = selectDR.GetFieldValue<string>(6);
                        string  nomeComune = selectDR.GetFieldValue<string>(7);
                        string  ragSoc = selectDR.GetFieldValue<string>(8);
                        string  cKompass = selectDR.GetFieldValue<string>(9);
                        int     idAzienda = selectDR.GetFieldValue<int>(10);
                        string  piva = selectDR.GetFieldValue<string>(11);
                        string  formaGiuridica = selectDR.GetFieldValue<string>(12);
                        if (ragSoc.Length == 0) continue;
                        System.Console.WriteLine(idAzienda + " " + ragSoc + " " + piva + " - " + nomeComune + " " + nomeProvincia);
                        lbAzienda.Text = idAzienda + " " + ragSoc + " " + piva + " - " + nomeComune + " " + nomeProvincia;
                        this.Refresh();
                        AcqGuideMDatas agm = new AcqGuideMDatas(25000, ragSoc,);
                        if (agm.getPIVA()=="") // if PIVA has been found continue with the next item in selectDR
                        {
                            continue;
                        }
                        // Else update the DB
                        updateCmd.Parameters["@IDAZIENDA"].Value = idAzienda;
                        updateCmd.Parameters["@PIVA"].Value = (string)agm.getPIVA();
                        updateCmd.ExecuteNonQuery();
                    }
                }
                catch (NullReferenceException exception)
                {
                    MessageBox.Show("btnUpdateGuidEM_Click - NullReferenceException: " + exception.Message + "\n");
                }
                catch (InvalidOperationException exception)
                {
                    MessageBox.Show("btnUpdateGuidEM_Click - InvalidOperationException: " + exception.Message + "\n");
                }
                catch (System.Data.SqlClient.SqlException exception)
                {
                    MessageBox.Show("btnUpdateGuidEM_Click - SqlException: " + exception.Message + "\n"+ exception.ErrorCode + "\n"+ exception.Number);
                }
                catch (System.Data.SqlTypes.SqlNullValueException exception)
                {
                    MessageBox.Show("btnUpdateGuidEM_Click - SqlNullValueException: " + exception.Message);
                }
            }
        }
        catch (System.Data.SqlClient.SqlException exception)
        {
            MessageBox.Show("btnUpdateGuidEM_Click - SqlException: " + exception.Message + "\n"+ exception.ErrorCode + "\n"+ exception.Number + "\n\n"+ selectSQL);
        }
        awConnection.Close();
    }
}

POST UPDATE

根据您的建议,我尝试添加连接以进行更新,如下所示。

using (SqlConnection updateConnection = new SqlConnection(this.connectionString))
{
    SqlCommand updateCmd = new SqlCommand(updateSQL, updateConnection);
    updateCmd.Parameters.Add("@IDAZIENDA", SqlDbType.Int);
    updateCmd.Parameters.Add("@PIVA", SqlDbType.Text);
    updateConnection.Open();
    updateCmd.Parameters["@IDAZIENDA"].Value = idAzienda;
    updateCmd.Parameters["@PIVA"].Value = (string)agm.getPIVA();
    updateCmd.ExecuteNonQuery();
}

它不起作用,我还有同样的问题,超时。是因为我正在尝试更新由前面的SELECT锁定的表吗? 再次感谢

1 个答案:

答案 0 :(得分:0)

您正在尝试更新当前正在阅读的表AGAZIENDE,这是一个锁定问题。一个简单的解决方法是用DataTable填充SqlDataAdapter,循环它的行并从那里更新。然后没有锁。

另一种方法,使用WITH(NOLOCK)

String selectSQL = @"
    SELECT
        AGAZIENDE.RAGSOC,
        AGAZIENDE.IDAZIENDA,
     FROM
        AGAZIENDE WITH(NOLOCK)
     WHERE 
        AGAZIENDE.IDAZIENDA>@STARTBY
        AND LEN(AGAZIENDE.PIVA)<>11
        AND AGAZIENDE.VALID = 1
     ORDER BY AGAZIENDE.IDAZIENDA";