2014-02-16 43 views
0

我有一個方法(帶WebMethod屬性),I定義一個事務,在我的方法,在我的交易我請2個存儲過程,第一個是GetAllBook爲什麼我的交易不起作用?

select * 
from Book_TBL 
where IsActive = 'True' 

和第二個是updateBook

update Book_TBL 
set IsActive = 'False' 
where IsActive = 'True' 

和我的方法:

public struct BOOK 
{ 
     public string BOOK_NAME; 
     public string BOOK_DESC;   
} 

[WebMethod] 
public static List<BOOK> GetMyBooks() 
{ 
    using (TransactionScope _transactionScope = new TransactionScope(TransactionScopeOption.Required)) 
    { 
     string _connString = "Data Source=.;Initial Catalog=BookStore;Integrated Security=True"; 
     SqlConnection _conn = new SqlConnection(_connString); 
     _conn.Open(); 

     SqlCommand _com = new SqlCommand(); 
     _com.CommandType = System.Data.CommandType.StoredProcedure; 
     _com.CommandText = "GetAllBook"; 
     _com.Connection = _conn; 

     SqlDataAdapter bookdataAdapter = new SqlDataAdapter(_com); 
     DataSet bookDS = new DataSet(); 
     bookdataAdapter.Fill(bookDS, "Book_TBL"); 

     List<BOOK> bookList = new List<BOOK>(); 
     _conn.Close(); 

     BOOK book; 

     foreach (DataRow dr in bookDS.Tables["Book_TBL"].Rows) 
     { 
      book = new BOOK(); 
      book.BOOK_NAME = dr["book_name"].ToString(); 
      book.BOOK_DESC = dr["book_desc"].ToString(); 
      bookList.Add(book); 
     } 

     SqlCommand updateCommand= new SqlCommand(); 
     _conn.Open(); 
     updateCommand.CommandText = "updateBook"; 
     updateCommand.CommandType = System.Data.CommandType.StoredProcedure; 
     updateCommand.Connection = _conn; 
     updateCommand.ExecuteNonQuery(); 
     _conn.Close(); 

     return bookList; 
    } 
} 

當我運行該項目myMethod給我的書籍列表有IsActive = True但它沒有更新我的表!問題是什麼?

+0

您是否看到任何錯誤? – Mutant

回答

0

你應該使用類SqlTransaction這樣的:

using (SqlConnection cn = new SqlConnection()) 
{ 
    cn.Open(); 
    using (SqlTransaction tr = cn.BeginTransaction()) 
    { 
     //some code 
     tr.Commit(); 
    } 
} 
1

我不沒什麼兩樣東西是你真的想用這樣的深層嵌套的方法做,但你首先應該把一切都到位:

public class Book 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

    public static List<Book> GetMyBooks() 
    { 
     var bookList = new List<Book>(); 
     using (var transactionScope = new TransactionScope(TransactionScopeOption.Required)) 
     { 
      const string connString = "Data Source=.;Initial Catalog=BookStore;Integrated Security=True"; 
      using (var conn = new SqlConnection(connString)) 
      { 
       using (var com = new SqlCommand()) 
       { 
        com.CommandType = CommandType.StoredProcedure; 
        com.CommandText = "GetAllBook"; 
        com.Connection = conn; 

        using (var bookdataAdapter = new SqlDataAdapter(com)) 
        { 
         using (var bookDataSet = new DataSet()) 
         { 
          bookdataAdapter.Fill(bookDataSet, "Book_TBL"); 

          foreach (DataRow dr in bookDataSet.Tables["Book_TBL"].Rows) 
          { 
           bookList.Add(new Book 
           { 
            Name = dr["book_name"].ToString(), 
            Description = dr["book_desc"].ToString() 
           }); 
          } 

          using (var updateCommand = new SqlCommand()) 
          { 
           updateCommand.CommandText = "updateBook"; 
           updateCommand.CommandType = CommandType.StoredProcedure; 
           updateCommand.Connection = conn; 
           updateCommand.ExecuteNonQuery(); 
          } 
         } 
        } 
       } 
      } 

      transactionScope.Complete(); 

      return bookList; 
     } 
    } 

而且如上所述,您必須手動提交事務。

+0

感謝您的建議 – pejman