1

I want to store multiple rows at once in SQL Server. I have taken one list datatype which is storing multiple row content. Now I want to pass this list directly to SQL Server. I don't want to use for loop to insert value one by one. How can I pass the whole list as a parameter to my stored procedure?

2

2 Answers 2

0

You can use a comma-separated string, and parse it on sql side. That is the fastest solution, so you will not have an open connection wile doing the loop on c# side.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use the concept of Batch Update or SQLBulkCopy or you can pass Array as Parameter to Command Object.

You can also Pass the data in XML format to SQL Server Stored Procedure .

Or Simplest way create multiple appended strings for inserts as below:

 string sqlText = "INSERT INTO Person VALUES ( '" + txtPersonId1.Text.Trim() + "',' " +  txtPersonName1.Text + "'); "; 
    sqlText += "INSERT INTO Person VALUES ( '" + txtPersonId2.Text.Trim() + "',' " +  txtPersonName2.Text + "'); "; 
    sqlText += "INSERT INTO Person VALUES ( '" + txtPersonId3.Text.Trim() + "',' " +  txtPersonName3.Text + "'); "; 
    sqlText += "INSERT INTO Person VALUES ( '" + txtPersonId4.Text.Trim() + "',' " +  txtPersonName4.Text + "'); "; 

All These Methods are explained over here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.