E
engahmedbarbary
Guest
I work on csharp app .net core visual studio 2017 I write the following function but I dont know
are this is implement transaction success or not ?
if not correct what is wrong or remaining to be correct ?
public static bool WithTransaction=true;
public static IDbCommand cmd;
public static IDbTransaction _transaction;
static Providers _Provider;
public static DataTable ExecuteDataTablesp(string sql, SqlConnection dbConnection, DbParameter[] @params = null, CommandType cmdType = CommandType.StoredProcedure)
{
SqlCommand cmd1 = null;
SqlDataAdapter DA1 = null;
if (dbConnection == null)
{
if (WithTransaction)
dbConnection = (SqlConnection)BeginTransaction();
else
dbConnection = (SqlConnection)InitializeConnection();
}
switch (Provider)
{
case Providers.SQLServer:
cmd1 = new SqlCommand("", dbConnection as SqlConnection);
DA1 = new SqlDataAdapter("", dbConnection as SqlConnection);
if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)
{
cmd1.Transaction = (SqlTransaction)_transaction;
cmd1.Connection = (SqlConnection)_transaction.Connection;
}
break;
default:
break;
}
if (sql == "") return new DataTable();
DataSet ds = new DataSet();
lock (synObj)
{
sql = AnalyizeBooleanFields(sql);
cmd1.CommandText = sql;
cmd1.CommandType = cmdType;
cmd1.Parameters.Clear();
cmd1.CommandType = cmdType;
if (@params != null && @params.Length > 0)
{
cmd1.Parameters.AddRange(@params);
}
if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)
{
cmd1.Transaction = (SqlTransaction)_transaction;
cmd1.Connection = (SqlConnection)_transaction.Connection;
}
else
{
if (string.IsNullOrEmpty(dbConnection.ConnectionString))
dbConnection.ConnectionString = CreateConnectionString(CurrentDataBase);
if (dbConnection.State != ConnectionState.Open)
dbConnection.Open();
}
cmd1.CommandTimeout = 0;
DA1.SelectCommand = cmd1;
DA1.Fill(ds);
if (!WithTransaction && dbConnection.State == ConnectionState.Open)
dbConnection.Close();
}
return ds.Tables[0];
}
Continue reading...
are this is implement transaction success or not ?
if not correct what is wrong or remaining to be correct ?
public static bool WithTransaction=true;
public static IDbCommand cmd;
public static IDbTransaction _transaction;
static Providers _Provider;
public static DataTable ExecuteDataTablesp(string sql, SqlConnection dbConnection, DbParameter[] @params = null, CommandType cmdType = CommandType.StoredProcedure)
{
SqlCommand cmd1 = null;
SqlDataAdapter DA1 = null;
if (dbConnection == null)
{
if (WithTransaction)
dbConnection = (SqlConnection)BeginTransaction();
else
dbConnection = (SqlConnection)InitializeConnection();
}
switch (Provider)
{
case Providers.SQLServer:
cmd1 = new SqlCommand("", dbConnection as SqlConnection);
DA1 = new SqlDataAdapter("", dbConnection as SqlConnection);
if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)
{
cmd1.Transaction = (SqlTransaction)_transaction;
cmd1.Connection = (SqlConnection)_transaction.Connection;
}
break;
default:
break;
}
if (sql == "") return new DataTable();
DataSet ds = new DataSet();
lock (synObj)
{
sql = AnalyizeBooleanFields(sql);
cmd1.CommandText = sql;
cmd1.CommandType = cmdType;
cmd1.Parameters.Clear();
cmd1.CommandType = cmdType;
if (@params != null && @params.Length > 0)
{
cmd1.Parameters.AddRange(@params);
}
if (WithTransaction && _transaction.Connection != null && _transaction.Connection.State == ConnectionState.Open)
{
cmd1.Transaction = (SqlTransaction)_transaction;
cmd1.Connection = (SqlConnection)_transaction.Connection;
}
else
{
if (string.IsNullOrEmpty(dbConnection.ConnectionString))
dbConnection.ConnectionString = CreateConnectionString(CurrentDataBase);
if (dbConnection.State != ConnectionState.Open)
dbConnection.Open();
}
cmd1.CommandTimeout = 0;
DA1.SelectCommand = cmd1;
DA1.Fill(ds);
if (!WithTransaction && dbConnection.State == ConnectionState.Open)
dbConnection.Close();
}
return ds.Tables[0];
}
Continue reading...