I'm writing a program for viewing, archiving and restoring SQL-Data.
The problem occurs only in restoring and while using target framework .NET 3.5
.
.NET 4.0
and higher seem to have solved the issue, but I have to write the program with 3.5
to guarantee the full compatibility with the computers where it should be used on.
The following method is called a few times (5-10) in order to get all names of a databases tables:
public List<string> GetAllTables(string sDatabase)
{
List<string> result = new List<string>();
try
{
using (SqlConnection con = new SqlConnection(dbConnection.conString + sDatabase))
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT name FROM sys.Tables", con))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(reader["name"].ToString());
}
}
}
}
return result;
}
catch (SqlException sqlEx)
{
string methodName = (new System.Diagnostics.StackTrace()).GetFrame(0).GetMethod().Name;
string className = (new System.Diagnostics.StackTrace()).GetFrame(0).GetMethod().DeclaringType.Name;
Debug.Print("Error in class {0} - method: {1}: {2}", className, methodName, sqlEx.Message);
return result;
}
}
The error occurs at var reader = command.ExecuteReader()
. The weird thing is that it doesn't happen every time. Every second restoring is successful and doesn't throw the error.
The exception stack trace is like this:
Error in class DatabaseWorker - method: GetAllTables: Transmission-level error when sending the request to the server. (provider: TCP-Provider, error:0 - An existing connection was closed by the remote host.)
at System.Data.SqlClient.SqlConnection.onError(SQLException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.WriteSni()
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
at System.Data.SqlClient.TdsParser.TdsExecuteSQLBatch(String text, Int32 timeout, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
for sql_data archiving.fashion model.DatabaseWorker.GetAllTables(String sDatabase)