JT_SIRO
asked on
Best way to import Excel sheet into Multiple SQL Server tables
I'm building a data import that reads in an Excel doc then inserts into my SQL Server tables using ASP.NET.
My data structure is relational, having a parent record called Metadata, that can have 1 to 5 Publisher and Composer records. I writing my code now and want to know if this is the best way. See below. I'm looping through the Excel rows, inserting into Metadata, getting the RecID of the insert, then check the Excel doc for Publisher and Composers, and insert if they are there. The code seems really cumbersome to me, but I can't think of a better way. Moreover, how to I write it so if any insert fails for the row, the other inserts won't be committed?
My data structure is relational, having a parent record called Metadata, that can have 1 to 5 Publisher and Composer records. I writing my code now and want to know if this is the best way. See below. I'm looping through the Excel rows, inserting into Metadata, getting the RecID of the insert, then check the Excel doc for Publisher and Composers, and insert if they are there. The code seems really cumbersome to me, but I can't think of a better way. Moreover, how to I write it so if any insert fails for the row, the other inserts won't be committed?
foreach (DataRow row in dt.Rows)
{
try
{
strSQL = "INSERT INTO metadata (Filename, TrackTitle, Time, BPM, Description, Keywords, Category, SubCategory, FeaturedInstrument, Mood, IsVocal, CDTitle, OldFilename, Username, Initials, status, RealityGenre, RealitySubGenre) " +
"VALUES ('" + CleanIt(row["Filename"].ToString(), 254) + "', '" + CleanIt(row["TrackTitle"].ToString(), 128) +
"', '" + CleanIt(row["Time"].ToString(),50) + "', '" + CleanIt(row["BPM"].ToString(), 31) +
"', '" + CleanIt(row["Description"].ToString(), 500) + "', '" + CleanIt(row["Keywords"].ToString(), 500) +
"', '" + CleanIt(row["Category"].ToString(),62) + "', '" + CleanIt(row["SubCategory"].ToString(), 62) +
"', '" + CleanIt(row["FeaturedInstrument"].ToString(), 128) + "', '" + CleanIt(row["Mood"].ToString(), 254) +
"', '" + CleanIt(row["IsVocal"].ToString(), 50) + "', '" + CleanIt(row["CDTitle"].ToString(), 254) +
"', '" + CleanIt(row["OldFilename"].ToString(), 254) + "', '" + CleanIt(row["Username"].ToString(), 50) +
"', '" + CleanIt(row["Initials"].ToString(), 10) + "', '" + CleanIt(row["status"].ToString(), 15) +
"', '" + CleanIt(row["RealityGenre"].ToString(), 100) + "', '" + CleanIt(row["RealitySubGenre"].ToString(), 100) + "')";
SqlCommand cmdIns = new SqlCommand(strSQL, cn);
int RecID = cmdIns.ExecuteNonQuery();
if (row["Composer1"] != "")
{
// Insert the 1st composer into Composers table
}
if (row["Composer2"] != "")
{
// Insert the 2nd composer into Composers table
}
if (row["Composer3"] != "")
{
// Insert the 3rd composer into Composers table
}
if (row["Composer4"] != "")
{
// Insert the 4th composer into Composers table
}
if (row["Composer5"] != "")
{
// Insert the 5th composer into Composers table
}
if (row["Publisher1"] != "")
{
// Insert the 1st Publisher into Publishers table
}
if (row["Publisher2"] != "")
{
// Insert the 2nd Publisher into Publishers table
}
if (row["Publisher3"] != "")
{
// Insert the 3rd Publisher into Publishers table
}
if (row["Publisher4"] != "")
{
// Insert the 4th Publisher into Publishers table
}
if (row["Publisher5"] != "")
{
// Insert the 5st Publisher into Publishers table
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SSIS is really simple. you dont realy have to code anything crazy. a quick search for SSIS Export Data will get you started. in SSIS you will specify a SOURCE (Excel File) and a Destination(Sql Table)..Map the Columns and thats it you are done.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER