Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for inserting data into SQL Server database using Enterprise library

I am new to C# Windows Application coding and using Enterprise library.

I want to insert records into SQL Server 2008 database using Enterprise Library 4.1

I am getting confused between SQLCommand and DBCommand which one to use and when to use.

like image 458
Amruta Avatar asked May 28 '11 07:05

Amruta


3 Answers

You don't need to use EntLib, just use old good ADO.NET:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "INSERT INTO table (column) VALUES (@param)";

    command.Parameters.AddWithValue("@param", value);

    connection.Open();
    command.ExecuteNonQuery();
}

Here's the class signature on MSDN:

public sealed class SqlCommand : DbCommand, ICloneable

SqlCommand derives from DbCommand

like image 109
abatishchev Avatar answered Oct 21 '22 17:10

abatishchev


DbCommand (in the System.Data.Common namespace) is an abstract base class from which SqlCommand, OleDbCommand, OdbcCommand. OracleCommand, etc all are derived.

like image 31
Ravi Parekh Avatar answered Oct 21 '22 19:10

Ravi Parekh


SQLcommand is a subclass of DBcommand. Use SQLcommand if you are connecting to SQL server

like image 42
Christian Hagelid Avatar answered Oct 21 '22 17:10

Christian Hagelid