I am trying to code a method where a user can pass multiple sql statements in string array format. and create a transaction. Rollback if nessesary. At the moment I get an error saying that the transaction needs a command to execute. Any advice would be appreciated, Maybe another(or right) way of doing what I need done. The following is the existing code.
public bool Scalar(params string[] sqlTexts)
{
SqlTransaction tran = null;
try
{
using (SqlConnection connection = new SqlConnection(strConnectionString)) // Auto dispose of connection
{
connection.Open(); // Open the connection
using (tran = connection.BeginTransaction())
{
using (SqlCommand cmdCommand = connection.CreateCommand()) // Auto dispose of command
{
foreach (string currentSQL in sqlTexts)
{
cmdCommand.CommandText = currentSQL;
cmdCommand.ExecuteScalar();
}
}
tran.Commit();
}
}
return true;
}
catch (Exception)
{
if (tran != null)
tran.Rollback();
return false;
}
}