Monday, 15 February 2010

c# - How to bind array data to SqlCommand.Parameter? -



c# - How to bind array data to SqlCommand.Parameter? -

it's possible perform operation without "foreach" ???

i understand if there library functions associate parameter list of info , perform n insert together

string sql_detail = "insert [importdetail] " + " ([idmaster] " + " ,[operation] " + " ,[data]) " + " values " + " (@a,@b,@c) "; foreach (importdetail imp in this.lstimportdetail ) { sqlcommand dettcommand = new sqlcommand(sql_detail, mytrans.connection); dettcommand.transaction = mytrans; dettcommand.parameters.add("a", sqldbtype.int).value = imp.idmaster; dettcommand.parameters.add("b", sqldbtype.nvarchar).value = imp.operation; dettcommand.parameters.add("c", sqldbtype.nvarchar).value = imp.data; =i+ (int)dettcommand.executenonquery(); }

thank help

if feeling frisky, create user-defined table type , set info importdetail objects datatable. wouldn't totally alleviate need loop, though -- makes more efficient since you're transforming info in memory instead of passing remote database. 1 time have datatable populated match schema of table type, can pass parameter stored procedure handle inserts. procedure can made transactional if you'd like, , since input info in table (of sorts), sql stored procedure should pretty simple, well.

for more info on user-defined table types , how they're used in .net, check out link: http://msdn.microsoft.com/en-us/library/bb675163.aspx

going little further, if have quite often, it's not out of realm of possibility turn sort of extension method on ienumerable maintain things dry.

is sort of solution looking for? it's little hard me tell given wording of question.

c# sql .net sql-server sqlcommand

No comments:

Post a Comment