0

In my .NET (C#) win application, I import an Excel file that contains multiple columns of varying numbers. So, I store the data in my datatable in code and I need to pass that datatable to a SQL Server stored procedure for further processing.

Is there any proper way to pass my datatable to a SQL Server stored procedure and can user defined table type have a dynamic number of columns?

2
  • you can create a global temp table and access it from the stored procedure. Commented Nov 21, 2019 at 5:52
  • @SerkanArslan can you please share more details about global temp table and how do i access. Thanks Commented Nov 21, 2019 at 6:00

2 Answers 2

1

if it's possible to create a table type with a dynamic number of columns.

No, its not possible.

My suggestion would be you can update the data from datatable directly to sql server table like this, then use the table in your procedure.

using (var bulkCopy = new SqlBulkCopy(_connection.ConnectionString, SqlBulkCopyOptions.KeepIdentity))
{
      // my DataTable column names match my SQL Column names, so I simply made this loop. However if your column names don't match, just pass in which datatable name matches the SQL column name in Column Mappings
      foreach (DataColumn col in table.Columns)
      {
          bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
      }

      bulkCopy.BulkCopyTimeout = 600;
      bulkCopy.DestinationTableName = destinationTableName;
      bulkCopy.WriteToServer(table);
}

Note, datatable column name should be same as your sql table column name to get it mapped properly.

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

5 Comments

My problem is i dont have fixed columns, my columns can be differ each time i import excel.
@SyedWahhab - Do you know all possible column names ? Each time it could be different columns present in excel, but do you have the full list of possible columns
I have a property data and their borrowers can be multiple one property may have Two borrowers and other property may have 15 borrowers, lets assume a maximum number of borrowers (50) at a single property, should i create a 50 columns and then utilize as per availability of data? PS: I cannot pass data in comma separated values for multiple column
@SyedWahhab - Yeah, thats the idea.
According to me, you should use JSON to store data in database. You don't need to create multiple columns for borrowers. Create a column in table which will be of size max and store all your borrowers in JSON format in that column wrt property.
0

First of all, you can not create a table with a dynamic number of columns.

You can use JSON to store all your borrowers in a single column. You really don't need to create multiple columns for borrowers. You can simply create a column in a table that will be of size NVARCHAR(MAX) and you have to create a JSON of all borrowers and store it in a table.

While retrieving data from the table deserialize it to get in original form.

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.