Saturday, May 4, 2013

Insert CSV file into generate DB table

Hello !!- This function at the time was one of the hardest one's I ever created because I was given a two hour window for development and testing.  So if that's what your currently facing this function will do the job.  Basically the way it works is, first we create a data table and then read the csv file into it. Next create a db table, this can be the name of the file that's been uploaded, or whatever you want to call it. Next, we need to loop through the data table until it reaches 0.  While the loop is running, it's going to create headers as well rows in the data table.  Finally, when it starts to create the new db table. It will go though the data table, grab the row headers and set them to the db table headers then create a new row for each row in the data table.  Now, I've created this function in the Page_Load event, but this can be broken down into separate methods and improve this function to handle more conditions and situations you may be having.  Hopefully this will get you on the right track and hopefully I was able to break this concept down enough to easily understand.  I've included a link to my function. Good Luck :-)

private string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
        DataTable dt = new DataTable();
        string line = null;
        int i = 0;
     
        using (StreamReader sr = File.OpenText(@"c:\myfile.csv"))
        {
            while ((line = sr.ReadLine()) != null)
            {
                string[] data = line.Split(',');
                if (data.Length > 0)
                {
                    if (i == 0)
                    {
                        foreach (var item in data)
                        {
                            dt.Columns.Add(new DataColumn(item)); 
                        }
                        i++;
                    }
                    DataRow row = dt.NewRow();
                    row.ItemArray = data;
                    dt.Rows.Add(row);
                }
            }
        }
        CreateDatabaseTable(dt, "ImportedData3");

        var connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity & SqlBulkCopyOptions.KeepNulls);
        using (bulk)
        {
            bulk.ColumnMappings.Add(new SqlBulkCopyColumnMapping(0, 0));
            bulk.BatchSize = 25;
            bulk.DestinationTableName = "ImportedData3";
            bulk.WriteToServer(dt);
        }
        DataHelper.DeleteTableRow("ImportedData3");
    }
    private void CreateDatabaseTable(DataTable dt, string tableName)
    {
        string sqlQuery = string.Empty;
        string sqlDBType = string.Empty;
        string dataType = string.Empty;
        int maxLength = 0;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat(string.Format("CREATE TABLE {0} (", tableName));
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            dataType = dt.Columns[i].DataType.ToString();
            if (dataType == "System.Int32")
            {
                sqlDBType = "int";
            }
            else if (dataType == "System.String")
            {
                sqlDBType = "nvarchar";
                maxLength = 500;
            }
            if (maxLength > 0)
            {
                sb.AppendFormat(string.Format(" [{0}] {1} ({2}), ", dt.Columns[i].ColumnName, sqlDBType, maxLength));
            }
            else
            {
                sb.AppendFormat(string.Format(" [{0}] {1}, ", dt.Columns[i].ColumnName, sqlDBType)); 
            }
        }
        sqlQuery = sb.ToString();
        sqlQuery = sqlQuery.Trim().TrimEnd(',');
        sqlQuery = sqlQuery + " )";
        using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            sqlConn.Open();
            SqlCommand sqlCmd = new SqlCommand(sqlQuery, sqlConn);
            sqlCmd.ExecuteNonQuery();
            sqlConn.Close();
        }
    }

No comments:

Post a Comment