J
Jan Waiz
Hi All,
i am a little bit confused whats wrong with my Code - be so kind and have a
look at this:
I have to do a lot of Insert-Statements into a SQL-Database after few
Select-Statements. This should run in a Transaction. I have implement this
as (in short):
// = = = BOF
oSelConn = new SqlConnection(...);
oSelConn.Open();
oSelTrans = oSelConn.BeginTransaction();
try
{
oSelCmnd = oSelConn.CreateCommand();
oSelCmnd.Transaction = oSelTrans;
oSelCmnd.CommandText = "SELECT * FROM MyTable";
// Get Pk-Value for further Needs
oSelRead = oSelCmnd.ExecuteReader();
sPkValue = oSelRead["PkColumn"]
oSelRead.Close()
// Delete all Records in DataTable for PkValue
oSelCmnd.CommandText = "SELECT * FROM MyData WHERE MyData.PK_MyTable =
'" + sPkValue + "'";
oSelCmnd.ExecuteNonQuery();
// Now a Bulk of Insert
while ( true )
{
oSelCmnd.CommandText =
"INSERT INTO MyData " +
"(PK_MyTable,PK_MyData)" +
" VALUES " +
"(@PKM,@PKD)";
oSelCmnd.Parameters.Add( "@PKM", sPkValue )
oSelCmnd.Parameters.Add( "@PKD", Guid.NewGuid().ToString() )
// HERE IT CRASH - READ BELOW
oSelCmnd.ExecuteNonQuery();
}
}
catch
{ oSelTrans.Rollback(); }
finally
{ oSelConn.close(); }
// = = = EOF
I get an Error about Duplicate-Key Unique-Constraint >>PK_MyData<< ???????
I have fix it (do i?) with generating a new Connection-Objekt and assigning
the existing Transaction-Objekt to the new Command-Object of the
Insert-Connection like this:
while ( true )
{
oInsConn = new SqlConnection(...);
oInsConn.Open();
oInsCmnd = oSelConn.CreateCommand();
oInsCmnd.Transaction = oSelTrans; // <= Existing
Transaction-Object
oInsCmnd.CommandText =
"INSERT INTO MyData " +
"(PK_MyTable,PK_MyData)" +
" VALUES " +
"(@PKM,@PKD)";
oInsCmnd.Parameters.Add( "@PKM", sPkValue )
oInsCmnd.Parameters.Add( "@PKD", Guid.NewGuid().ToString() )
// HERE IT CRASH - READ BELOW
oInsCmnd.ExecuteNonQuery();
oInsConn.Close();
}
Am i right ?
Thanks a lot in Advance for your Time to read this Stuff and your Help
Regards
Jan Waiz
i am a little bit confused whats wrong with my Code - be so kind and have a
look at this:
I have to do a lot of Insert-Statements into a SQL-Database after few
Select-Statements. This should run in a Transaction. I have implement this
as (in short):
// = = = BOF
oSelConn = new SqlConnection(...);
oSelConn.Open();
oSelTrans = oSelConn.BeginTransaction();
try
{
oSelCmnd = oSelConn.CreateCommand();
oSelCmnd.Transaction = oSelTrans;
oSelCmnd.CommandText = "SELECT * FROM MyTable";
// Get Pk-Value for further Needs
oSelRead = oSelCmnd.ExecuteReader();
sPkValue = oSelRead["PkColumn"]
oSelRead.Close()
// Delete all Records in DataTable for PkValue
oSelCmnd.CommandText = "SELECT * FROM MyData WHERE MyData.PK_MyTable =
'" + sPkValue + "'";
oSelCmnd.ExecuteNonQuery();
// Now a Bulk of Insert
while ( true )
{
oSelCmnd.CommandText =
"INSERT INTO MyData " +
"(PK_MyTable,PK_MyData)" +
" VALUES " +
"(@PKM,@PKD)";
oSelCmnd.Parameters.Add( "@PKM", sPkValue )
oSelCmnd.Parameters.Add( "@PKD", Guid.NewGuid().ToString() )
// HERE IT CRASH - READ BELOW
oSelCmnd.ExecuteNonQuery();
}
}
catch
{ oSelTrans.Rollback(); }
finally
{ oSelConn.close(); }
// = = = EOF
I get an Error about Duplicate-Key Unique-Constraint >>PK_MyData<< ???????
I have fix it (do i?) with generating a new Connection-Objekt and assigning
the existing Transaction-Objekt to the new Command-Object of the
Insert-Connection like this:
while ( true )
{
oInsConn = new SqlConnection(...);
oInsConn.Open();
oInsCmnd = oSelConn.CreateCommand();
oInsCmnd.Transaction = oSelTrans; // <= Existing
Transaction-Object
oInsCmnd.CommandText =
"INSERT INTO MyData " +
"(PK_MyTable,PK_MyData)" +
" VALUES " +
"(@PKM,@PKD)";
oInsCmnd.Parameters.Add( "@PKM", sPkValue )
oInsCmnd.Parameters.Add( "@PKD", Guid.NewGuid().ToString() )
// HERE IT CRASH - READ BELOW
oInsCmnd.ExecuteNonQuery();
oInsConn.Close();
}
Am i right ?
Thanks a lot in Advance for your Time to read this Stuff and your Help
Regards
Jan Waiz