SqlConnection conn
=
null
;
SqlTransaction trans = null ;
try
{
conn = new SqlConnection( "" ); // 连库,略
SqlCommand cmd = new SqlCommand( " INSERT INTO test(title) VALUES (' " + txtTitle.Text + " ') " ,conn);
trans = conn.BeginTransaction();
cmd.Transaction = trans;
int rows = cmd.ExecuteNonquery();
if (rows > 0 )
{
trans.Commit();
MessageBox.Show( " OK " , " OK " );
}
}
catch (Exception e)
{
trans.Rollback();
MessageBox.Show( " Error " , " Error " )
}
finally
{
if (conn != null ){conn.Close();}
}
SqlTransaction trans = null ;
try
{
conn = new SqlConnection( "" ); // 连库,略
SqlCommand cmd = new SqlCommand( " INSERT INTO test(title) VALUES (' " + txtTitle.Text + " ') " ,conn);
trans = conn.BeginTransaction();
cmd.Transaction = trans;
int rows = cmd.ExecuteNonquery();
if (rows > 0 )
{
trans.Commit();
MessageBox.Show( " OK " , " OK " );
}
}
catch (Exception e)
{
trans.Rollback();
MessageBox.Show( " Error " , " Error " )
}
finally
{
if (conn != null ){conn.Close();}
}