代码如下,这个问题似乎是一个笑话,花费了我大量的时间。调用此方法时,我会发出一个请求,并找到多少4000条记录,其中一个字段PIVA为空。因此,我调用一个函数,它使用函数agm.getPIVA()询问站点,有时在那里找到一个PIVA。所以我会更新db,但是有一个问题,更新请求会导致SQLException超时。如您所见,操作非常短,只涉及1(1)条记录。其他的东西?IDAZIENDA是一个主键,总表大约有30.000行。有人怀疑,由于某些原因,我不能以这种方式使用相同的连接,但我复制了一个可以工作的旧函数,这似乎是一样的。连接字符串启用了火星,所以我有点困惑,理论上这个问题不应该出现。
任何建议都将受到欢迎。提前谢谢。
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();
}
}后更新
根据您的建议,我尝试添加一个连接进行更新,如下所示。
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锁定的表吗?再次感谢
发布于 2017-10-05 14:10:19
您正在尝试更新当前正在读取的表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";https://stackoverflow.com/questions/46587422
复制相似问题