C# SQL connection timeout -
i have strange case of sql connection timeout application written in c# .net.
the sqlcommand.executenonquery()
being used sequentially execute several scripts in sql server, 1 after another. each script contains command create 1 table (no data update/insert/delete operations @ all). reason, @ 1 of scripts, sqlcommand.executenonquery
throws timeout exception.
when execute creation of these tables in sql server management studio, executed fine , instantaneously.
does has idea causing timeout when tables created application?
all sql scripts similar following:
sql:
create table dbo.test ( code varchar(10) not null , name varchar(50) not null , other columns... primary key , unique key , foreign key )
the scripts shipped c# using code:
try { using (sqlconnection consql = new sqlconnection ("[connection string]")) { using (sqlcommand cmdsql = new sqlcommand(ssql, consql)) { cmdsql.commandtimeout = itimeout; consql.open(); cmdsql.executenonquery(); // jumps catch part , // throws out timeout exception consql.close(); } } } catch(exception ex) { throw (ex); }
this happening on test server, meaning nothing else happening on server while application executing these scripts.
you can override default time out setting sql transactions updating machine.config
, can found here:
%windir%\microsoft.net\framework\[version]\config\machine.config
64-bit
%windir%\microsoft.net\framework64\[version]\config\machine.config
at end of machine.config
add or update following line:
<system.transactions> <machinesettings maxtimeout="01:00:00" /> --> set desired value. </system.transactions> </configuration>
if above doesn't work, can specify timeout setting
sqlcommand
through code well:
using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); sqlcommand command = new sqlcommand(querystring, connection); // setting command timeout in seconds: command.commandtimeout = 3600; try { command.executenonquery(); } catch (sqlexception e) { console.writeline(e); } }
more information here
Comments
Post a Comment