Code for inserting data into SQL Server database u

2020-08-26 10:37发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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



回答4:

you can first try using SQLCommand - example here

and refer this if you want further information on SQL and DBCommand.