How use Transaction with more than functions using c# - c#

How use Transaction with more than functions using c#
for example
i have three function
//first function
save();//------to save data-----
//second function
saveDetailes(); //-----to save detiales ----------
//third function
updateStauts(); //--------to update onother table ---------------
I want to ensure that all of them are implemented or not implemented using TransAction
thanks

after alot of tries and search alot of resorces i was found a solution for my problem
solution by
passing a sqlcommand to all functions and return from every fuction as aboolean value is save done return true
if three function return true
transaction be commit other ways transaction roolback
thanks

If I get it correctly you need to use a common SqlTransaction in multiple methods. This is how I do it. First you have to gather all of your methods into a common one. Then you pass your one SqlConenction and SqlTransaction to all of your methods and return a boolean flag to notify your main method whether your queries were sucessfully or not.
using System.Data.SqlClient;
namespace SqlTransationDemo
{
class Program
{
static void Main(string[] args)
{
//Do your sql logic here
DoSomething();
}
private static bool DoSomething()
{
try
{
using (var connection = new SqlConnection("SqlConnectionString"))
{
connection.Open();
//If not commited, transaction is rolled-back as soon as it is disposed
using (var transaction = connection.BeginTransaction())
{
//Either use a false loop to break or throw an exception. Your choice.
do
{
if (!Foo1(connection, transaction))
break;
if (!Foo2(connection, transaction))
break;
if (!Foo3(connection, transaction))
break;
//Commit
transaction.Commit();
return true;
}
while (false);
}
}
}
catch
{
return false;
}
}
private static bool Foo1(SqlConnection Connection, SqlTransaction Transaction)
{
try
{
using (var command = new SqlCommand())
{
command.Connection = Connection;
command.Transaction = Transaction;
command.CommandText = "Query1";
command.ExecuteNonQuery();
}
return true;
}
catch
{
return false;
}
}
private static bool Foo2(SqlConnection Connection, SqlTransaction Transaction)
{
try
{
using (var command = new SqlCommand())
{
command.Connection = Connection;
command.Transaction = Transaction;
command.CommandText = "Query2";
command.ExecuteNonQuery();
}
return true;
}
catch
{
return false;
}
}
private static bool Foo3(SqlConnection Connection, SqlTransaction Transaction)
{
try
{
using (var command = new SqlCommand())
{
command.Connection = Connection;
command.Transaction = Transaction;
command.CommandText = "Query3";
command.ExecuteNonQuery();
}
return true;
}
catch
{
return false;
}
}
}
}

Related

Returning Type SqlDataReader even when try catch fails - getting not all paths return a value

I'm trying to read from a DB with a try/catch and I want to obviously return data. I'm getting a message saying not all code paths return a value. I get that I need to return something, but how should I handle this scenario? Can I return an empty SqlDataReader object?
The stored procedures being run will return different types of objects depending on what's executed, otherwise I would return the specific type.
private SqlDataReader RunSql(string connectionString, string procName)
{
using(SqlConnection = conn = new SqlConnection(connectionString))
{
using(SqlCommand SqlCmd = new SqlCommant(procName, conn))
{
SqlCmd.CommandType = CommandType.StoredProcedure;
conn.Open();
try
{
using(SqlDataReader reader = SqlCmd.ExecuteReader())
{
if(reader.HasRows)
{
return reader;
}
}
}
catch(Exception e)
{
//?
}
finally
{
conn.Close();
}
}
}
//?
}
The accepted answer will work but, as written, it "swallows" the exception that you caught; this prevents the caller from even knowing that something went awry. A much better approach is to rethrow the exception so that the caller is still informed that something bad happened and then they can then deal with the issue however they want to.
Another issue is that the reader and connection objects will be disposed of before you ever consume them in downstream code; thanks to the using and finally statements. One solution to this issue is to make it the responsibility of the caller to handle the connection object.
private static SqlDataReader RunSql(SqlConnection connection, string procedureName) {
using (var command = connection.CreateCommand()) {
command.CommandText = procedureName;
command.CommandType = CommandType.StoredProcedure;
try {
if (connection.State != ConnectionState.Open) {
connection.Open();
}
var reader = command.ExecuteReader();
if (reader.HasRows) {
return reader;
}
else {
return null;
}
}
catch (Exception e) {
// log the exception or whatever you wanna do with it
throw; // rethrow the exception
}
}
}
Here's an alternative version that uses IAsyncEnumerable; notice that we're now allowed to dispose of the objects within the same method. Also, the logic that handled the empty reader case is no longer required as our enumerable will simply be empty if there was nothing to read.
// define a container for information relevant to each row
public sealed class SqlResultSetRow : IEnumerable<(string fieldName, Type fieldType, object fieldValue)>
{
private readonly (string fieldName, Type fieldType, object fieldValue)[] m_fields;
public int ResultSetIndex { get; }
public SqlResultSetRow((string, Type, object)[] fields, int resultSetIndex) {
m_fields = fields;
ResultSetIndex = resultSetIndex;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<(string fieldName, Type fieldType, object fieldValue)> GetEnumerator() {
var rows = m_fields;
foreach (var row in rows) {
yield return row;
}
}
public object GetFieldName(int fieldOffset) => m_fields[fieldOffset].fieldName;
public object GetFieldValue(int fieldOffset) => m_fields[fieldOffset].fieldValue;
}
// define a class to hold our generic method(s)
public static class SqlClientExtensions
{
// implement a refactored version of the original method
public static async IAsyncEnumerable<T> ProcessRows<T>(this SqlCommand command, Func<SqlResultSetRow, CancellationToken, ValueTask<T>> rowCallback, CommandBehavior commandBehavior = CommandBehavior.SequentialAccess, [EnumeratorCancellation] CancellationToken cancellationToken = default) {
using (var dataReader = await command.ExecuteReaderAsync(commandBehavior, cancellationToken)) {
var resultSetIndex = 0;
do {
var fieldCount = dataReader.FieldCount;
var fieldNames = new string[fieldCount];
var fieldTypes = new Type[fieldCount];
for (var i = 0; (i < fieldCount); ++i) {
fieldNames[i] = dataReader.GetName(i);
fieldTypes[i] = dataReader.GetFieldType(i);
}
while (await dataReader.ReadAsync(cancellationToken)) {
var fields = new (string, Type, object)[fieldCount];
for (var i = 0; (i < fieldCount); ++i) {
fields[i] = (fieldNames[i], fieldTypes[i], dataReader.GetValue(i));
}
yield return await rowCallback(new SqlResultSetRow(fields, resultSetIndex), cancellationToken);
}
} while (await dataReader.NextResultAsync(cancellationToken));
}
}
}
class Program
{
// a minimal implementation of a rowCallBack function
public static async ValueTask<ExpandoObject> OnProcessRow(SqlResultSetRow resultSetRow, CancellationToken cancellationToken) {
var rowValue = (new ExpandoObject() as IDictionary<string, object>);
foreach (var field in resultSetRow) {
rowValue[field.fieldName] = field.fieldValue;
}
return (rowValue as ExpandoObject);
}
// put everything together
static async Task Main(string[] args) {
try {
var programTimeout = TimeSpan.FromMinutes(3);
var cancellationTokenSource = new CancellationTokenSource(programTimeout);
using (var connection = new SqlConnection(#"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;"))
using (var command = connection.CreateCommand()) {
command.CommandText = "select 1 as [a];";
command.CommandType = CommandType.Text;
await connection.OpenAsync(cancellationToken: cancellationTokenSource.Token);
await foreach (dynamic row in command.ProcessRows(rowCallback: OnProcessRow, cancellationToken: cancellationTokenSource.Token)) {
Console.WriteLine($"a: {row.a}");
}
}
}
catch (Exception e) {
// do something with exception here
throw;
}
}
}
As the method doesn't return anything if the condition failed this is the reason you are getting that compile error. You could try bellow snippet. Just return null on your commented line //?
private SqlDataReader RunSql(string connectionString, string procName)
{
using (var conn = new SqlConnection(connectionString))
{
using (SqlCommand SqlCmd = new SqlCommand(procName, conn))
{
SqlCmd.CommandType = CommandType.StoredProcedure;
conn.Open();
try
{
using (SqlDataReader reader = SqlCmd.ExecuteReader())
{
if (reader.HasRows)
{
return reader;
}
}
}
catch (Exception e)
{
}
finally
{
conn.Close();
}
}
}
return null;
}
Hope that might resolve your compile error.
{
SqlCmd.CommandType = CommandType.StoredProcedure;
conn.Open();
try
{
using(SqlDataReader reader = SqlCmd.ExecuteReader())
{
if(reader.HasRows)
{
return reader;
}
}

Proper way to handle connection and transaction in case of Get and Save operations

I am having a method like below where i perform save operations in 3 tables :
public void Do
{
using (var myConnection = new SqlConnection(connectionString))
{
for (int i = 0; i < 4; i++)
{
MyProcess.Execute(myConnection);//Dispose connection
}
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
using (var transaction = myConnection.BeginTransaction())
{
MyClass cls = new MyClass(myConnection,transaction);
// Here i have 3 insert operation
try
{
myRepo.Save1(myConnection,transaction);
myRepo.Save2(myConnection,transaction);
myRepo.Save3(myConnection,transaction);
transaction.Commit();
}
catch
{
transaction.Rollback();
}
}
}
}
}
Now in above approach i have created transaction so that if insert fails in 1 table then it shoudl roll back for other tables too.
Now in MyClass there are again some get data operations for which i need to pass transaction because if i dont do that i was getting below error :
ExecuteScalar requires the command to have a transaction when the
connection assigned to the command is in a pending local transaction.
The Transaction property of the command has not been initialized.
Above error was coming from GetOperations of MyClass and thats the reason why i have created transaction in the beginning and then passing to MyClass where i have some Get Operations from database tables
So my question is shall i restrict my transaction to Save1,Save2 and Save3 only and have all Get Operations inside MyClass to open and close their own connection?
Update :
public class MyClass
{
SqlConnection myConnection;
SqlTransaction transaction;
public MyClass(SqlConnection myConnection, SqlTransaction transaction)
{
this.myConnection = myConnection;
this.transaction = transaction;
}
public int GetEmployee(int id, int MockProfileId, string ProfileVersion)
{
var myEmp = new EmployeeRepo();
int basic = myEmp.GetEmployeeBasic(myConnection,transaction,100);
//Other code for get operations like GetEmployeeBasic
}
}
public class EmployeeRepo
{
public int GetEmployeeBasic(SqlConnection connection,SqlTransaction transaction,int id)
{
string query = "";
using (SqlCommand cmd = new SqlCommand(query, connection, transaction))
{
cmd.Parameters.AddWithValue("#id", id);
var data = cmd.ExecuteScalar();
if (data != null)
return (int)data;
else
return 0;
}
}
}

C# Mysql Connection must be valid and open

First of all: I got my code running without using oop. I declared all my variables inside the same class and opened/closed the connection right before and after passing the query to the db. That worked! Now with some new experiences I tried to split my code into different classes. Now it wont work anymore.
It tells me "Connection must be valid and open". Enough text, here's my current code:
Services.cs
public static MySqlConnection conn // Returns the connection itself
{
get
{
MySqlConnection conn = new MySqlConnection(Services.ServerConnection);
return conn;
}
}
public static string ServerConnection // Returns the connectin-string
{
get
{
return String.Format("Server={0};Port=XXXX;Database=xxx;Uid=xxx;password=xxXxxXxXxxXxxXX;", key);
}
}
public static void DB_Select(string s, params List<string>[] lists)
{
try
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
string command = s;
cmd.CommandText = command;
MySqlDataReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
if (sqlreader[0].ToString().Length > 0)
{
for (int i = 0; i < lists.Count(); i++)
{
lists[i].Add(sqlreader[i].ToString());
}
}
else
{
foreach (List<string> save in lists)
{
save.Add("/");
}
}
}
sqlreader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
}
}
LoginForm.cs
private void checkUser(string username, string password)
{
using (Services.conn)
{
Services.conn.Open();
Services.DB_Select("..a short select statement..");
Services.conn.Close();
}
I guess this is all we need. I have shortened my code to get a focus on the problem.
I created Services.cs to get a global way to access the db from all forms without copy&pasting the connection info. Now when I reach my LoginForm.cs it throws an error "Connection must be valid and open". I've already debugged my code. It's all time closed. Even when passing conn.Open() it stays closed. Why?
Another try: I've also tried placing conn.Open() and conn.Close() inside Services.DB_Select(..) at the beginning and end. Same error here.
I have to say: The code worked before and I've used the same connection-string. So the string itself is surely valid.
I appreciate any help given here!
The problem is that you don't store the connection that was returned from your factory property. But don't use a property like a method. Instead use it in this way:
using (var con = Services.conn)
{
Services.conn.Open();
Services.DB_Select("..a short select statement..", con ));
//Services.conn.Close(); unnecessary with using
}
So use the same connection in the using that was returned from the property(or better created in the using) and pass it to the method which uses it. By the way, using a property as factory method is not best practise.
But in my opinion it's much better to create the connection where you use it, best place is in the using statement. And throw the con property to the garbage can, it is pointless and a source for nasty errors.
public static void DB_Select(string s, params List<string>[] lists)
{
try
{
using(var conn = new MySqlConnection(Services.ServerConnection))
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = s;
using( var sqlreader = cmd.ExecuteReader())
while (sqlreader.Read())
{
if (sqlreader[0].ToString().Length > 0)
{
for (int i = 0; i < lists.Count(); i++)
{
lists[i].Add(sqlreader[i].ToString());
}
}
else
{
foreach (List<string> save in lists)
{
save.Add("/");
}
}
} // unnecessary to close the connection
} // or the reader with the using-stetement
}
catch (Exception ex)
{
MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
}
}
Try to restructure your Services class as follows
public static MySqlConnection conn // Returns the connection itself
{
get
{
MySqlConnection conn = new MySqlConnection(Services.ServerConnection);
return conn;
}
}
private static string ServerConnection // Returns the connectin-string - PRIVATE [Improved security]
{
get
{
return String.Format("Server={0};Port=XXXX;Database=xxx;Uid=xxx;password=xxXxxXxXxxXxxXX;", key);
}
}
// Rather than executing result here, return the result to LoginForm - Future improvement
public static void DB_Select(MySqlConnection conn ,string s, params List<string>[] lists)
{
try
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
string command = s;
cmd.CommandText = command;
MySqlDataReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
if (sqlreader[0].ToString().Length > 0)
{
for (int i = 0; i < lists.Count(); i++)
{
lists[i].Add(sqlreader[i].ToString());
}
}
else
{
foreach (List<string> save in lists)
{
save.Add("/");
}
}
}
sqlreader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error while selecting data from database!\nDetails: " + ex);
}
}
In LoginForm.cs use returning connection and store it there. When you need to execute query, use
MySqlConnection conn=Services.conn(); // Get a new connection
Services.DB_Select(conn,"..a short select statement.."); // Executing requirement
Services.conn.Close();
Additional - I suggest you need to return MySqlDataReader to LoginForm and handle results there
private MySqlConnection _conn;
public MySqlConnection conn // Returns the connection itself
{
get
{
if(_conn == null)
_conn = new MySqlConnection(Services.ServerConnection);
return _conn;
}
}

How do i use SqlTransaction across multipe methods

Let's assume we have an Object A which can be deleted an Object B which hold a forendkey from A
If you want to deleted A, you have to delete the forendkey from B first and then you can delete A but if something goes wrong it should be rolled back but i also want to use the delete forendkey from B independent but at the moment i don't know how to achieve this
my current idea :
public void DeleteA(Object a)
{
using (SqlConnection con = new SqlConnection())
{
con.open();
using (SqlTransaction tr = con.BeginTransaction())
{
try
{
DeleteAfromAllB(a, con, tr);
using (SqlCommand cmd = new SqlCommand("STP_A_Delete", con))
{
cmd.Transaction = tr;
// some parameters
// some sort of Execute
// e.g.: cmd.ExecuteNonQuery();
}
tr.Commit();
}
catch (SqlException ex)
{
//ExceptionHandling
}
}
}
}
private void DeleteAfromAllB(Object a, SqlConnection con, SqlTransaction tr)
{
try
{
using (SqlCommand cmd = new SqlCommand("STP_B_Delete_Referenc_To_A", con))
{
cmd.Transaction = tr;
// some parameters
// some sort of Execute
// e.g.: cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
//ExceptionHandling
}
}
public void DeleteAfromAllB(Object a)
{
using (SqlConnection con = new SqlConnection())
{
con.open();
using (SqlTransaction tr = con.BeginTransaction())
{
DeleteAfromAllB(a,con,tr);
tr.Commit();
}
}
}
but like you can see this is pretty ugly
The call
public void DeleteAfromAllB(Object a)
does not need to pass the SqlConnection as you can reference from tr.Connection. So you just need the SqlTransaction as parameter. So for your original question, yes I think passing in the SqlTransaction is the way to go. Personally I prefer this way because you can easily trace the call stack / scope of the transaction (i.e. where the transaction started/finished).
Another alternative is to use a TransactionScope.
E.g.
private void DeleteAfromAllB(Object a)
{
using (var con = new SqlConnection())
{
con.open();
using (var cmd = new SqlCommand("STP_B_Delete_Referenc_To_A", con))
{
// some parameters
// some sort of Execute
// e.g.: cmd.ExecuteNonQuery();
}
}
}
public void DeleteAfromAllB_TopLevel(Object a)
{
using (var scope = new TransactionScope())
{
try
{
DeleteAfromAllB(a);
// The Complete method commits the transaction. If an exception has been thrown,
// Complete is not called and the transaction is rolled back.
scope.Complete();
}
catch (Exception)
{
//ExceptionHandling
}
}
}

Using MySQLConnection in C# does not close properly

I try to write a class to make MySql Connections easier. My problem is, after I open a connection and close it. It is still open in the Database and gets aborted.
I'm using the 'using' statement' of course, but the connection is still open and gets aborted after I exit the program.
Here's what my code looks like:
using (DatabaseManager db = new DatabaseManager())
{
using (MySqlDataReader result = db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
{
foreach (MySqlDataReader result in db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
{
//Do stuff here
}
}
}
The class Database manager opens the connection and closes it when disposed:
public DatabaseManager()
{
this.connectionString = new MySqlConnectionStringBuilder("Server=localhost;Database=businessplan;Uid=root;");
connect();
}
private bool connect()
{
bool returnValue = true;
connection = new MySqlConnection(connectionString.GetConnectionString(false));
connection.Open();
}
public void Dispose()
{
Dispose(true);
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
connection.Dispose();
}
}
//GC.SuppressFinalize(this);//Updated
}
//Updated
//~DatabaseManager()
//{
// Dispose(false);
//}
So, I checked it in the debugger and the Dispose()-method is called and executes correctly.
What am I missing? Is there something I did wrong or misunderstood?
Just in case, the DataReader()-method (Updated version):
public IEnumerable<IDataReader> DataReader(String query)
{
using (MySqlCommand com = new MySqlCommand())
{
com.Connection = connection;
com.CommandText = query;
using (MySqlDataReader result = com.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
{
while (result.Read())
{
yield return (IDataReader)result;
}
}
}
}
Ok, I tried to use the yield return:
foreach (MySqlDataReader result in db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
{
//...
}
And I changed the DataReader-method:
public IEnumerable<IDataReader> DataReader(String query)
{
using (MySqlCommand com = new MySqlCommand())
{
com.Connection = connection;
com.CommandText = query;
using (MySqlDataReader result = com.ExecuteReader())
{
while (result.Read())
{
yield return (IDataReader)result;
}
}
}
}
It works in the way that I can retrieve the data, yet I still have the same problem: The connection isn't closed properly.
Im unsure about mysqlconnection but the sql server counter part uses Connection pooling and does not close when you call close instead it puts it in the connection pool!
Edit: Make sure you dispose the Reader, Command, and Connection object!
Edit: Solved with the ConnectionString Parameter "Pooling=false" or the static methods MySqlConnection.ClearPool(connection) and MySqlConnection.ClearAllPools()
You need to wrap the Command and the DataReader in using statements as well.
According to the mysql docs, the MySQLConnection is not closed when it goes out of scope. Therefore you must not use it inside a using.
Quote...
"If the MySqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling MySqlConnection.Close or MySqlConnection.Dispose."
Have a look at using something like this:
private static IEnumerable<IDataRecord> SqlRetrieve(
string ConnectionString,
string StoredProcName,
Action<SqlCommand> AddParameters)
{
using (var cn = new SqlConnection(ConnectionString))
using (var cmd = new SqlCommand(StoredProcName, cn))
{
cn.Open();
cmd.CommandType = CommandType.StoredProcedure;
if (AddParameters != null)
{
AddParameters(cmd);
}
using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (rdr.Read())
yield return rdr;
}
}
}

Categories