ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.4k

are transaction implemented success on this function or not

Jun 1 2020 7:48 PM
I work on csharp function I make enhance to function by add transaction but i dont know
 
transaction implemented success or it have some thing wrong :
 
I need answer acording to logic :
 
  1. public static DataTable ExecuteDataTablesp(string sql, SqlConnection dbConnection, DbParameter[] @params = null, CommandType cmdType = CommandType.StoredProcedure)  
  2.    {  
  3.        SqlCommand cmd1 = null;  
  4.        SqlDataAdapter DA1 = null;  
  5.          
  6.            if (dbConnection == null)  
  7.            {  
  8.                if (WithTransaction)  
  9.                    dbConnection = (SqlConnection)BeginTransaction();  
  10.                else  
  11.                    dbConnection = (SqlConnection)InitializeConnection();  
  12.            }  
  13.   
  14.   
  15.            switch (Provider)  
  16.            {  
  17.                case Providers.SQLServer:  
  18.   
  19.                    cmd1 = new SqlCommand("", dbConnection as SqlConnection);  
  20.                    DA1 = new SqlDataAdapter("", dbConnection as SqlConnection);  
  21.                    if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)  
  22.                    {  
  23.                        cmd1.Transaction = (SqlTransaction)_transaction;  
  24.                        cmd1.Connection = (SqlConnection)_transaction.Connection;  
  25.                    }  
  26.                    break;  
  27.   
  28.   
  29.                default:  
  30.                    break;  
  31.            }  
  32.            if (sql == ""return new DataTable();  
  33.            DataSet ds = new DataSet();  
  34.        try  
  35.        {  
  36.            lock (synObj)  
  37.            {  
  38.   
  39.                sql = AnalyizeBooleanFields(sql);  
  40.                cmd1.CommandText = sql;  
  41.                cmd1.CommandType = cmdType;  
  42.                cmd1.Parameters.Clear();  
  43.   
  44.                if (@params != null && @params.Length > 0)  
  45.                {  
  46.                    cmd1.Parameters.AddRange(@params);  
  47.                   
  48.                }  
  49.                if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)  
  50.                {  
  51.                    cmd1.Transaction = (SqlTransaction)_transaction;  
  52.                    cmd1.Connection = (SqlConnection)_transaction.Connection;  
  53.                }  
  54.                else  
  55.                {  
  56.                    if (string.IsNullOrEmpty(dbConnection.ConnectionString))  
  57.                        dbConnection.ConnectionString = CreateConnectionString(CurrentDataBase);  
  58.                    if (dbConnection.State != ConnectionState.Open)  
  59.                        dbConnection.Open();  
  60.                }  
  61.                //  if (WithTransaction) cmd1.Transaction = _transaction;  
  62.                cmd1.CommandTimeout = 0;  
  63.                DA1.SelectCommand = cmd1;  
  64.                DA1.Fill(ds);  
  65.   
  66.                if (!WithTransaction && dbConnection.State == ConnectionState.Open)  
  67.                    dbConnection.Close();  
  68.   
  69.   
  70.            }  
  71.            if (WithTransaction)  
  72.            {  
  73.                _transaction.Commit();  
  74.            }  
  75.        }  
  76.        catch  
  77.        {  
  78.            _transaction.Rollback();  
  79.        }  
  80.        if (WithTransaction &&  _transaction.Connection.State == ConnectionState.Open)  
  81.        {  
  82.            dbConnection.Close();  
  83.        }  
  84.        return ds.Tables[0];  
  85.    }  
 

Answers (3)