I've used TransactionScope in the past without issue and I'm kind of stumped as to why it's not working in this new unit test project.
My code is trivial. The InsertQuote() method inserts info into 3 tables. It throws an exception inserting into the 3rd table, but the inserts from the first two tables remain. The SQL code is entirely encapsulated within the InsertQuote() method (meaning there's no global SqlConnection object or anything.
public void LoadQuote()
{
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.Serializable }))
{
InsertQuote("TICK", "NASDAQ", DateTime.Parse("1/1/2010"), 10.42m, 10.47m, 30.32m, 1.41m, 10.46m, 1234567, false);
ts.Complete();
}
}
private void InsertQuote(string ticker, string exchange, DateTime quoteDate, decimal open, decimal current, decimal high, decimal low, decimal close, int volume, bool intraday)
{
int exchangeId = InsertExchange(exchange);
int companyId = InsertCompany(ticker, ticker, ticker, exchangeId);
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(_sqlInsertQuote, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@CompanyId", companyId);
cmd.Parameters.AddWithValue("@QuoteDate", quoteDate);
cmd.Parameters.AddWithValue("@OpenPrice", open);
cmd.Parameters.AddWithValue("@CurrentPrice", current);
cmd.Parameters.AddWithValue("@High", high);
cmd.Parameters.AddWithValue("@Low", low);
cmd.Parameters.AddWithValue("@ClosePrice", close);
cmd.Parameters.AddWithValue("@Volume", volume);
cmd.Parameters.AddWithValue("@Intraday", intraday);
cmd.ExecuteNonQuery();
}
}
}
The InsertExchange() and InsertCompany() methods look similar to the structure of the InsertQuote() method.
My connection string is:
Server=localhost\SQLEXPRESS;Database=StockDB;Trusted_Connection=True;Enlist=false
Don't know what could be the problem.
Aucun commentaire:
Enregistrer un commentaire