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

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

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

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

您不需要使用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(); } 

这是MSDN上的类签名:

 public sealed class SqlCommand : DbCommand, ICloneable 

SqlCommand派生自DbCommand

DbCommand (在System.Data.Common命名空间中)是一个抽象基类, SqlCommandOleDbCommandOdbcCommandOracleCommand等都是派生出来的。

SQLcommand是DBcommand的子类。 如果要连接到SQL Server,请使用SQL命令

你可以先尝试使用SQLCommand – 这里的例子

如果您想了解有关SQL和DBCommand的更多信息,请参阅此内容。