Friday, March 9, 2012

How is the Performance of the SQL with .Net?

Hi

I want to insert 1000s of records into SQL Server 2005 Database with some manipulation. So that i put into the For Loop and inserting record.

Inside the loop i am opening the connection and closing after use. The sample code is below

for(int i=0;i<1000;i++)
{

sqlCmd.CommandText = "ProcName";
sqlCmd.Connection = sqlCon;
sqlCmd.Connection.Open():
sqlCmd.ExecuteNonQuery();
sqlCmd.Connection.Close();

}

What my Question is.. How is the Performance of this Code..?? Will is take time to get the Connection and Close the Connection in every itration?

Or Shall I Open the Connection in Begining of the outside loop and close the connection at end of the Loop? will it increase the Performace?

Please clarify me these question.. Thanks in advance.

Hi,

Opening a connection and closing it takes a lot of extra resources, so use following.

1sqlCmd.CommandText ="ProcName";2sqlCmd.Connection = sqlCon;3sqlCmd.Connection.Open():45for(int i=0;i<1000;i++)6{7 sqlCmd.ExecuteNonQuery();8}910sqlCmd.Connection.Close();11

No comments:

Post a Comment