Please delete this post. I'm so ashamed of me... After hours of digging, it came out I did some tests in Management Studio, where I tested some transactions without committing them at all. So it was waiting for a commit, and I kept making, or trying to make inserts ... ! Shame on me ! Sorry about this.
Multiple Insert transaction connection timeout - ADO.NET - SQL Server
-
16-01-2022 - |
Question
I have a transaction with multiple inserts. All of the inserts work fine, except one. I verified the parameters, the spelling, all of it, and it seems I'm not figuring it out. It gives me the error:
Timeout expired. The timeout period elapsed prior to completion of the operation
or the server is not responding.
My transaction looks like this:
SqlConnection db = new SqlConnection(connString);
DataSet dataSet = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
using (db)
{
db.Open();
SqlTransaction trans = db.BeginTransaction();
try
{
//insert into COMMSignalDefinition !!Problem HERE
da.InsertCommand =
new SqlCommand("INSERT INTO COMMSignalDefinition(Name) "
+ "VALUES (@name)", db, trans);
da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
foreach (DataRow row in ds.Tables["COMMTerminalSignal"].Select())
{
da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
da.InsertCommand.ExecuteNonQuery();
}
// insert into COMMSignalExceptionDefinition -- names
da.InsertCommand =
new SqlCommand("INSERT INTO COMMSignalExceptionDefinition(Name) "
+ "VALUES (@name)", db, trans);
da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
foreach (DataRow row in ds.Tables["COMMSignalExceptionDef"].Select())
{
da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
da.InsertCommand.ExecuteNonQuery();
}
trans.Commit();
MessageBox.Show("You have successfully imported your Settings. "
+ "You can now exit the program.",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception e)
{
trans.Rollback();
MessageBox.Show(e.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
I have more inserts which work fine (I deleted the rest of them) and I moved the one with problems at the beginning.
My question is what could I possibly do wrong?
I even verified if the "problematic" query is sent to the server, with SQL Server Profiler,
and it does! And if I execute it in SQL Server Management studio
, it works too.
Connection Timeout
is set to 30
Can You please give me some leads ? SQL Server version is 2005 ! Thank You!
Solution
OTHER TIPS
If this is a pretty long running operation, you can try to change the Command Timeout.
- Check your connection string. Maybe you have some mistake in your connString var
- Check your SqlDataAdapter command timeouts. They may be the cause of the exception (see http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx) Usually such exception is the result of either long running tasks or uncommitted transactions.
Connection Timeout is set to 1
SqlConnection.ConnectionTimeout
Property
The time (in seconds) to wait for a connection to open. The default value is 15 seconds.
So you have set the timout to 1 second. Does that answer your question?
Remarks: You can set the amount of time a connection waits to time out by using the ConnectTimeout or Connection Timeout keywords in the connection string. A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.