none
Insert array data in database RRS feed

  • Question

  • there is an array of objects.

    I want to insert all the objects in database.

    how can I do that?

    Friday, July 5, 2019 10:06 AM

Answers

  • You could try something like this

            public void InsertData(MyData[] records)
            {
                string connectionString = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
                using(SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    foreach(var record in records)
                    {
                        SqlCommand insertCommand = new SqlCommand("Insert into myTable (Column1, Column2) values (@col1, @col2)", conn);
                        insertCommand.Parameters.AddWithValue("@col1", record.Column1);
                        insertCommand.Parameters.AddWithValue("@col2", record.Column2);
    
                        insertCommand.ExecuteNonQuery();
                    }
                    conn.Close();
                }
            }

    Monday, July 8, 2019 10:13 AM

All replies

  • Can you be more specific on what you are trying to do?  Do you have an array of objects and each item is a new record of the database?  Are you trying to put an array into a column in one row of the database?
    Saturday, July 6, 2019 9:23 AM
  • Yes, exactly I have an array of objects and each item is a new record of the database?   trying to put an array into a column in one row of the database?
    Monday, July 8, 2019 3:51 AM
  • Hi Shraddhesh Dongare, 

    Thank you for posting here.

    I hope the following reference can help you:

    How to insert the elements of an array into a SQL Server database using C#?

    If I have any misunderstanding, you can provide a detailed description about the question.This will help us quickly analyze your problem.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 8, 2019 6:59 AM
    Moderator
  • You could try something like this

            public void InsertData(MyData[] records)
            {
                string connectionString = "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;";
                using(SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    foreach(var record in records)
                    {
                        SqlCommand insertCommand = new SqlCommand("Insert into myTable (Column1, Column2) values (@col1, @col2)", conn);
                        insertCommand.Parameters.AddWithValue("@col1", record.Column1);
                        insertCommand.Parameters.AddWithValue("@col2", record.Column2);
    
                        insertCommand.ExecuteNonQuery();
                    }
                    conn.Close();
                }
            }

    Monday, July 8, 2019 10:13 AM
  • > how can I do that?

    Form a File/StringBuilder with SQL-INSERT instructions and execute them over connection to database.

    As option - form a CSV file and import this file into database. 

    P.S. Array of Objects - can be an array of mixed object types, but table in the database always are flat. 


    Sincerely, Highly skilled coding monkey.

    Monday, July 8, 2019 10:34 AM