You can do it multiple ways..
1) With Direct call form Source
foreach (Employee item in employees)
{
if (iCounter == 0)
{
cmd.BeginTransaction;
}
string sql = @"INSERT INTO Mytable (id, name, salary)
values ('@id', '@name', '@salary')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
iCounter ++;
if(iCounter >= 500)
{
cmd.CommitTransaction;
iCounter = 0;
}
}
if(iCounter > 0)
cmd.CommitTransaction;
2) Linq
var db = new StoreDataContext();
using(var scope = new TransactionScope()){
foreach(var emp in employee){
db.employee.Insert();
db.SubmitChanges(ConflictMode.FailOnFirstConflict);
}
scope.Complete();
}
3) Using Xml insert
Build an xml string, or convert the dataset to xml, and pass it to the database, and iterate in xml
CREATE PROCEDURE [dbo].[multipleInsert]
(
@xmlData NVARCHAR(max)
)
AS
BEGIN
INSERT INTO employee (id, name, dob)
SELECT
node.trnl.value('id', 'varchar(10)') id,
node.trnl.value('name', 'nvarchar(100)') name,
node.trnl.value('dob', 'nvarchar(8)') dob,
FROM @xml.nodes('/root/employee') node(trnl)
<root>
<employee id="00163500C6" xml:space="preserve">
<name>Rafeeq</name>
<dob>12-12-2011</dob>
</empoloyee>
</root>
END