Search code examples
sql-serverdataadapter

Is a Data Adapter always needed?


If have a query that I ONLY want to execute, and have no need to fill any data within a table, then is it necessary to have a data adapter for that purpose?


Solution

  • No it is not necessary to use a SqlDataAdapter. Its really a bridge between a DataSet and Sql Server.

    Instead you can use SqlCommand - ExecuteNonQuery.

    You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements. Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.

    using (SqlConnection conn = new SqlConnection(
               "your connection string"))
    {
          using (SqlCommand command = new SqlCommand("your sql", conn)
          {
            conn.Open();
            command.ExecuteNonQuery();
          }
    }