I need advice what is best approach to connect data between master and child table.
I have console application written in C# which scrape data from web, process data and insert them in table.
Master table with fields Id, OrdinalNumber, StringDate and child table with fields Id, OrdinalNumber, StringDate, Name, Amount, StartDate, EndDate.
Because I scrap data from site I don't have some unique identifier so I'm looking on Ordinal number (number from site) and StringDate(created like date but in string format ddMMyyyy
I scrap site ever day once daily).
While I insert data in table it is done like this:
INSERT INTO MasterTable(OrdinalNumber, StringDate) VALUES (@OrdinalNumber, @StringDate)
and after that I insert data in child table:
INSERT INTO ChildTable(OrdinalNumber, StringDate, Name, Amount, StartDate, EndDate)
VALUES(@OrdinalNumber, @StringDate, @Name, @Amount, @StartDate, @EndDate)
Is this okay approach or better approach would be to get Id from master table when I insert data call SELECT SCOPE_IDENTITY() to get last inserted Id and put them in child table with this query:
INSERT INTO ChildTable(MasterId, Name, Amount, StartDate, EndDate)
VALUES(@MasterId, @Name, @Amount, @StartDate, @EndDate)
My question is it better to stick with first approach or use SCOPE_IDENTITY() to get last ID and populate child data with it and save in database? I will insert approximately 10000 rows in Master table and 20000+ rows in child table.
I'm using dapper for inserting data in tables.
1.
IDbTransaction transaction = null;
try
{
using (IDbConnection connection = new SqlConnection(DbConnectionString))
{
if (connection.State != ConnectionState.Open)
connection.Open();
transaction = connection.BeginTransaction();
string sql = "INSERT INTO Master(OrdinalNumber, StringDate)" +
"VALUES(@OrdinalNumber, @StringDate)";
connection.Execute(sql, master, transaction);
string sql = "INSERT INTO Child(OrdinalNumber, StringDate, Name, Amount, StartDate, EndDate)" +
"VALUES(@OrdinalNumber, @StringDate, @Name, @Amount, @StartDate, @EndDate)";
connection.Execute(sql, child, transaction);
transaction.Commit();
}
}
catch (Exception ex)
{
if (transaction != null)
{
transaction.Rollback();
}
throw ex;
}
finally
{
if (transaction != null)
transaction.Dispose();
}
IDbTransaction transaction = null;
try { using (IDbConnection connection = new SqlConnection(DbConnectionString)) {
if (connection.State != ConnectionState.Open) connection.Open(); transaction = connection.BeginTransaction(); foreach(var m in master) { string sql = "INSERT INTO Master(OrdinalNumber, StringDate)" + "VALUES(@OrdinalNumber, @StringDate)" + "SELECT SCOPE_IDENTITY()"; var id = connection.ExecuteScalar<int>(sql, m, transaction); foreach(var c in child.Where(d => d.OrdinalNumber == m.OrdinalNumber)) { string sql = "INSERT INTO Child(MasterId, Name, Amount, StartDate, EndDate)" + "VALUES(@MasterId, @Name, @Amount, @StartDate, @EndDate)"; connection.Execute(sql, new {MasterId = id, c.Name, c.Amount, c.StartDate, c.EndDate}, transaction); } } transaction.Commit(); } } catch (Exception ex) { if (transaction != null) { transaction.Rollback(); } throw ex; } finally { if (transaction != null) transaction.Dispose(); }