Passing SQL Server connection around (node to c#) -
i working on node.js server. server uses edge.js execute c# workflow code (it dll).
we using custom written logging dll logs sql server table. node.js , workflow dll use logging dll log steps sql databse. ideally need open connection sql database once , reuse node , edge/c# code.
is there way of opening connection in node.js code , passing connection handle around edge/c#. or there better way this?
at moment opening connection when call c# workflow dll , closing @ end. doing in node causes problems (assuming because of async code) "system.invalidoperationexception: executenonquery requires open , available connection. connection's current state open.".
it bad idea pass sqlconnections around. partly because relatively expensive resource , may have limited number of them available. underlying database drivers automatically cache sqlconnections (afaik uses connection string key cache, use same connection string same data source).
you should use using statement ensure sqlconnections disposed in timely fashion, when no longer need them. e.g., wrap call executenonquery using statement creates/opens sqlconnection:
using (sqlconnection sqlconnection = new sqlconnection(connectionstring)) { sqlconnection.open(); // need define cmdtext. string cmdtext = ""; using (sqlcommand sqlcommand = new sqlcommand(cmdtext, sqlconnection)) { sqlcommand.commandtype = commandtype.text; sqlconnection.open(); var result = sqlcommand.executenonquery(); sqlconnection.close(); } }
Comments
Post a Comment