Step 1: Create a static method which will take datatable as parameter and extract all coulmns, its datatype. Then it generates sql quries. Here is the method:
public static string CreateTABLE(DataTable table)
{
string sqlQuery;
sqlQuery = "CREATE TABLE " + table.TableName + "(";
for (int i = 0; i < table.Columns.Count; i++)
{
sqlQuery += "\n [" + table.Columns[i].ColumnName + "] ";
string columnType = table.Columns[i].DataType.ToString();
switch (columnType)
{
case "System.Int32":
sqlQuery += " int ";
break;
case "System.Int64":
sqlQuery += " bigint ";
break;
case "System.Int16":
sqlQuery += " tinyint ";
break;
case "System.Decimal":
sqlQuery += " decimal ";
break;
case "System.DateTime":
sqlQuery += " datetime ";
break;
case "System.String":
default:
sqlQuery += string.Format(" nvarchar({0}) ", table.Columns[i].MaxLength == -1 ? "max" : table.Columns[i].MaxLength.ToString());
break;
}
if (table.Columns[i].AutoIncrement)
sqlQuery += " IDENTITY(" + table.Columns[i].AutoIncrementSeed.ToString() + "," + table.Columns[i].AutoIncrementStep.ToString() + ") ";
if (!table.Columns[i].AllowDBNull)
sqlQuery += " NOT NULL ";
sqlQuery += ",";
}
return sqlQuery.Substring(0, sqlQuery.Length-1) + "\n)";
}
DataTable yourDataTableObj = new DataTable();
string sqlQuery = CreateTABLE(yourDataTableObj);
Step 2: Once you call the method you will get SQL query. Collect it and execute it in sqlserver manually or you can execute through ADO.NET