private static readonly string dataProvider = ConfigurationManager.AppSettings.Get("Provider");
private static readonly DbProviderFactory factory = DbProviderFactories.GetFactory(dataProvider);
private static readonly string connectionString = ConfigurationManager.ConnectionStrings[dataProvider].ConnectionString;
/// <summary>
/// Executes Update statements in the database.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Number of rows affected.</returns>
public static int Update(string sql)
{
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = factory.CreateCommand())
{
command.Connection = connection;
command.CommandText = sql;
connection.Open();
return command.ExecuteNonQuery();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助重写这个,以便它可以使用存储过程.(通过sproc名称和params)有没有人知道我应该怎么做呢?编辑:我遇到问题的区域是试图找出填写参数的方法.
谢谢
假设我们有一个DAL方法
public void BuyProduct(int productId, int quantity, int buyerId);
Run Code Online (Sandbox Code Playgroud)
在该方法中,我们需要调用2个存储过程:
创建2个SqlCommands是一个好习惯 - 每个存储过程一个并使用单个SqlConnection来执行这些命令?
要么
为每个SqlCommand创建单独的SqlConnection是否更好?
所以基本上我要问:在单个DAL方法中重复使用单个SqlConnection进行多个(2-4)SqlCommands是一个好习惯(显然在整个DAL中重用SqlConnection会很愚蠢)?
PS - 请不要问我为什么不能将2个存储过程合并为1.我的答案是 - 分离关注点.
我为"菜鸟"问题提前道歉,但我在SQL的大多数方面仍然是一个新手.我的问题源于SQL的艺术第二章的一部分.在标题为"稳定数据库连接"的段落中,作者提到了将大量行插入数据库的几种方法.这是格式中的相应列表
Test; Results Connect/Disconnect for each line in turn; 7.4 lines loaded per second Connect Once, all candidate lines individually inserted; 1,681 lines loaded per second Connect Once, all candidate lines inserted in arrays of 10 lines; 5,914 lines inserted per second Connect Once, all candidate lines inserted in arrays of of 100 lines; 9,190 lines inserted per second
此外,作者还提到了"更快的直接加载技术".
不幸的是,我并不完全理解插入数组的想法(如果有的话).在插入数组和他引用的其他"直接加载技术"方面,有没有人可以详细说明作者所指的技术?
Hibernate和eclipselink是最受欢迎的Java DAL框架.根据我的理解,他们将程序员创建的对象映射到自动创建的数据库关系和存储过程.
使用此方法的优点和缺点是什么:
编写自己的DAL,假设我的项目是中小型的.
找到一个框架(如果存在?),它将以另一种方式 - 从数据库关系和存储过程创建类.
就个人而言,我对数据库关系而不是Java(或任何编程语言)类的推理感到更为自在.
c# ×2
.net-3.5 ×1
database ×1
eclipselink ×1
hibernate ×1
insert ×1
java ×1
refactoring ×1
sql ×1
t-sql ×1