in RowsetImportEngine/TextRowsetImporter.cs [355:428]
private void CreateTable ()
{
string SqlStmt = string.Empty;
SqlCommand cmd;
int len;
SqlConnection conn = new SqlConnection(this.connStr);
try
{
// Create the CREATE TABLE command.
cmd = new SqlCommand();
SqlStmt = "IF OBJECT_ID ('" + CurrentRowset.Name + "') IS NULL CREATE TABLE [" + CurrentRowset.Name + "] (";
foreach (RowsetImportEngine.Column c in CurrentRowset.Columns)
{
len = c.SqlColumnLength;
string ColumnName = c.Name.Trim();
SqlStmt += "[" + ColumnName + "] " + c.DataType.ToString();
// Add column length to those datatypes that need it
switch (c.DataType)
{
case SqlDbType.Decimal:
SqlStmt += "(38, 10)"; // we can't know the necessary prec/scale in advance -- use (38,10) as a compromise
break;
case SqlDbType.Float:
SqlStmt += "(53)"; // always use max float
break;
case SqlDbType.VarChar:
case SqlDbType.VarBinary:
if (len > 0 && len <= 8000)
SqlStmt += "(" + len + ")";
else if (len == Column.SQL_MAX_LENGTH || len > 8000)
SqlStmt += "(max)";
else
SqlStmt += "(" + DEFAULT_NONTAB_COLUMN_LEN + ")";
break;
case SqlDbType.NVarChar:
if (len > 0 && len <= 4000)
SqlStmt += "(" + len + ")";
else if (len == Column.SQL_MAX_LENGTH || len > 4000)
SqlStmt += "(max)";
else
SqlStmt += "(" + DEFAULT_NONTAB_COLUMN_LEN + ")";
break;
}
SqlStmt += " NULL, "; // Make all columns NULLable
}
// Remove the last comma.
SqlStmt = SqlStmt.TrimEnd(',', ' ') + ") \n";
/*
// No clustered index -- slows things down.
SqlStmt += "CREATE INDEX cidx ON [" + CurrentRowset.Name + "] "
+ "([" + (CurrentRowset.Columns[0] as Column).Name + "])";
*/
// Use the SqlCommand to run the CREATE TABLE.
conn.Open();
cmd.Connection = conn;
cmd.CommandText = SqlStmt;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
ErrorDialog ed = new ErrorDialog(e, false, this.logger);
Util.Logger.LogMessage("Createtable command " + SqlStmt);
ed.Handle();
}
finally
{
conn.Close();
}
return;
}