-1

Please advise as to choose between SqlCommand and SqlDataAdapter for SQL Server INSERT, UPDATE, and DELETE operations.

Let's consider the following code snippet for SQL Server:

public int Execute(SqlCommand commandSql) 

or

public int Insert(SqlCommand commandSql)

Considering the obvious advantage of SqlCommand (Execute method) is that it can be used for all INSERT, UPDATE, and DELETE operations and the advantage of SqlDataAdapter (Insert, Update, Delete methods) is disconnected architecture, what are the pros and cons of using SqlCommand ExecuteNonQuery vis-à-vis SqlDataAdapter ExecuteNonQuery?

Caveats for enterprise-class web applications? Please let experts clarify.

Please note that this question is not about LINQ or Entity Framework, this is about SqlCommand ExecuteNonQuery vis-à-vis SqlDataAdapter ExecuteNonQuery?

Important question: does SqlCommand ExecuteNonQuery use a connected or disconnected architecture?

//Insert
private bool Insert(string firstName, string lastName, string synonym)
{
    bool isInserted = false;
    try
    {
        int rowsAffected = 0;
        StringBuilder insertQuery = new StringBuilder();
        insertQuery.Append("INSERT INTO Customer ");
        insertQuery.Append("(");
        insertQuery.Append("FirstName, ");
        insertQuery.Append("LastName, ");
        insertQuery.Append("Synonym ");
        insertQuery.Append(") ");
        insertQuery.Append("VALUES ");
        insertQuery.Append("(");
        insertQuery.Append("@FirstName, ");
        insertQuery.Append("@LastName, ");
        insertQuery.Append("@Synonym ");
        insertQuery.Append(");");
        using (SqlCommand commandSql = new SqlCommand(insertQuery.ToString()))
        {
            commandSql.Parameters.AddWithValue("@FirstName", firstName);
            commandSql.Parameters.AddWithValue("@LastName", lastName);
            commandSql.Parameters.AddWithValue("@Synonym", synonym);
            DataAccessLayer dal = new DataAccessLayer();

            rowsAffected = dal.Execute(commandSql); // SqlCommand ????
        rowsAffected = dal.Insert(commandSql); // SqlDataAdapter ???

        }
        if (rowsAffected > 0)
        {
            isInserted = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return isInserted;
}
//UPDATE
private bool Update(int custId, string firstName, string lastName, string synonym)
{
    bool isUpdated = false;
    try
    {
        int rowsAffected = 0;
        StringBuilder updateQuery = new StringBuilder();
        updateQuery.Append("UPDATE Customer ");
        updateQuery.Append("SET ");
        updateQuery.Append("FirstName = @FirstName, ");
        updateQuery.Append("LastName = @LastName, ");
        updateQuery.Append("Synonym = @Synonym ");
        updateQuery.Append("WHERE ");
        updateQuery.Append("CustomerId = @CustomerId;");
        using (SqlCommand commandSql = new SqlCommand(updateQuery.ToString()))
        {
            commandSql.Parameters.AddWithValue("@CustomerId", custId);
            commandSql.Parameters.AddWithValue("@FirstName", firstName);
            commandSql.Parameters.AddWithValue("@LastName", lastName);
            commandSql.Parameters.AddWithValue("@Synonym", synonym);
            DataAccessLayer dal = new DataAccessLayer();
            rowsAffected = dal.Execute(commandSql); // SqlCommand ????
        rowsAffected = dal.Update(commandSql); // SqlDataAdapter ???
        }
        if (rowsAffected > 0)
        {
            isUpdated = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return isUpdated;
}
//DELETE
private bool Delete(int custId)
{
    bool isDeleted = false;
    try
    {
        int rowsAffected = 0;
        StringBuilder deleteQuery = new StringBuilder();
        deleteQuery.Append("DELETE FROM Customer ");
        deleteQuery.Append("WHERE ");
        deleteQuery.Append("CustomerId = @CustomerId;");
        using (SqlCommand commandSql = new SqlCommand(deleteQuery.ToString()))
        {
            commandSql.Parameters.AddWithValue("@CustomerId", custId);
            DataAccessLayer dal = new DataAccessLayer();
            rowsAffected = dal.Delete(commandSql);
        rowsAffected = dal.Execute(commandSql); // SqlCommand ????
        rowsAffected = dal.Delete(commandSql); // SqlDataAdapter ???
        }
        if (rowsAffected > 0)
        {
            isDeleted = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    return isDeleted;
}

// SqlCommand
public int Execute(SqlCommand commandSql)
{
    int rowsAffected = 0;
    using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
    {
        commandSql.Connection = connectionSql;
        commandSql.Connection.Open();
        rowsAffected = commandSql.ExecuteNonQuery();           
    }
    return rowsAffected;
}

// SqlDataAdapter
public int Insert(SqlCommand commandSql)
{
    int rowsAffected = 0;
    using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
    {
        using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
        {
            dataAdapterSql.InsertCommand = commandSql;
            dataAdapterSql.InsertCommand.Connection = connectionSql;
            dataAdapterSql.InsertCommand.Connection.Open();
            rowsAffected = dataAdapterSql.InsertCommand.ExecuteNonQuery();
        }            
    }
    return rowsAffected;
}
// SqlDataAdapter
public int Update(SqlCommand commandSql)
{
    int rowsAffected = 0;
    using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
    {       
        using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
        {
            dataAdapterSql.UpdateCommand = commandSql;
            dataAdapterSql.UpdateCommand.Connection = connectionSql;
            dataAdapterSql.UpdateCommand.Connection.Open();
            rowsAffected = dataAdapterSql.UpdateCommand.ExecuteNonQuery();
        }            
    }
    return rowsAffected;
}
// SqlDataAdapter
public int Delete(SqlCommand commandSql)
{
    int rowsAffected = 0;
    using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
    {
        using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
        {
            dataAdapterSql.DeleteCommand = commandSql;
            dataAdapterSql.DeleteCommand.Connection = connectionSql;
            dataAdapterSql.DeleteCommand.Connection.Open();
            rowsAffected = dataAdapterSql.DeleteCommand.ExecuteNonQuery();
        }
    }
    return rowsAffected;
}

Please help.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1660166
  • 79
  • 1
  • 6
  • 3
    Did you consider using LINQ to SQL or Entity Framework instead? – MarcinJuraszek Dec 28 '13 at 17:15
  • http://stackoverflow.com/questions/7777866/confused-between-sqlcommand-sqldataadapter and http://stackoverflow.com/questions/6569012/data-adapter-vs-sql-command are also dup contenders. – crthompson Dec 28 '13 at 17:23

1 Answers1

0

SQLDataAdapter is a specialized case for use with Dataset classes. If you are using plain SQL input as in your examples it would be better to use SQLCommand for this purpose if a specific dataset is not being used. If you are going to use something like a dataset for Enterprise use it would be worthwhile to consider the more updated Entity Framework instead as it provides typing versus the plain datasets.

Turnkey
  • 9,266
  • 3
  • 27
  • 36
  • Important question SqlCommand ExecuteNonQuery uses connected or disconnected architecture? – user1660166 Dec 28 '13 at 17:31
  • That is normally considered a "connected" architecture as it requires connection to modify the data. Datasets and Entity Framework are considered "disconnected" architectures as they can modify the data in memory and then sync back to the database. – Turnkey Dec 28 '13 at 19:07