使用Enterprise库将数据插入SQL Server数据库的代码

Amr*_*uta 8 .net c# sql-server ado.net sql-server-2008

我是C#Windows应用程序编码和使用企业库的新手.

我想使用Enterprise Library 4.1将记录插入SQL Server 2008数据库

我在SQLCommand和DBCommand之间混淆了哪一个使用以及何时使用.

aba*_*hev 7

您不需要使用EntLib,只需使用旧的好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();
}
Run Code Online (Sandbox Code Playgroud)

这是MSDN上的类签名:

public sealed class SqlCommand : DbCommand, ICloneable
Run Code Online (Sandbox Code Playgroud)

SqlCommand派生自DbCommand


Rav*_*ekh 5

DbCommand(在System.Data.Common命名空间)是一个抽象基类从中SqlCommand,OleDbCommand,OdbcCommand.OracleCommand等等都是派生的.