I want to know if there is any way to create a SQL query based on a C# datatable?
Using a dll I'm able to read data in an excel file, I have all sheets in a DataSet containing DataTables, every sheet is in a separate DatatTable.
I want to generate a query involving CREATE
clause to create a table and INSERT
clause to insert data into that table for every single sheet.
I don't know if it's possible by something like Entity Framework or not.
The way I can imagine, for Create
is to loop through DataSet and extract name of columns in every DataTable then add it in an string as query text. Here's what I mean:
- Extract table structure, suppose it's like following:
'ID' INT,
'Name' VARCHAR(5),
'SurName' VARCHAR(6),
'Age' INT
Create a DataRowModel:
public class DataRowModel { public string ColumnName { get; set; } public string ColumnType { get; set; } }
Populate list of DataRowModel by extracted data:
List<DataRowModel> TableModel = new List<DataRowModel>();
- USing a foreach I can reach to following query:
string query = @"CREATE TABLE IF NOT EXISTS Test (
'ID' INT,
'Name' VARCHAR(5),
'SurName' VARCHAR(6),
'Age' INT
);";
But it seems dirty, what is the recommended solution? In fact I want to create a tool, something like this cool website.