简体   繁体   中英

C#, SQL Server 2005 and missing session

I have a problem with a customer. I have this code:

var conn = new SqlConnection(Util.GetConnectionString());
var DataCommand = new SqlCommand();
var sql = "";

// subseccion
try
{
   sql = "TRUNCATE TABLE preview_" + tablename;
   DataCommand = new SqlCommand(sql, conn);
   DataCommand.Connection.Open();
   int numcol = DataCommand.ExecuteNonQuery();
   sql = "insert into  preview_" + tablename+ " select * from " + tablename;
   DataCommand = new SqlCommand(sql, conn);
   DataCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
   var latest_error = ex.Message;
   Util.Add_Event_Log(latest_error);
}
finally
{
   DataCommand.Dispose();
   if (conn.State == ConnectionState.Open)
   {
      conn.Close();
   }
   conn.Dispose();
}

This do the next thing, I give a name of a table, it TRUNCATE a table then copy the information from "table" to "preview_table" and it works as expected.

However, we found that if we don't give TRUNCATE permission for the table, it fail. But, my problem is that it does not only fail but also deleting the current session (and may be also restart the server process) .

My bet it is a server problem (server 2003) may be it is not patched or anything because I am working inside a try-catch part so it should not fail in this fashion.

My customers says the problem is in the code.

But I am not sure, maybe I should not a sql command in a chain.

Is this happening in the development environment as well as production environment? If so, you need to step through your code with the VS debugger and pin point the line at which the session is being deleted.

You should also check the event logs on the production server to see if they can provide any information.

As stated in the comments by msergey, it may be the Util.Add_Event_Log throwing an exception but you should test this by stepping through the code.

If it is Util.Add_Event_Log causing the issue, move this code out of the catch into its own try/catch statement by declaring an exception variable in the outer scope.

If it does wind up that the use of TRUNCATE is the culprit you might try swapping that out in favor of using a DELETE statement instead. Performance won't be as great, but you wouldn't require elevated user permissions in SQL Server either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM