我正在创建一个小的收银员应用程序,用于记录客户、员工、服务、销售和约会。我使用的是windows窗体,并且在该DataGrids中。我已经创建了要用于应用程序的数据库。我想知道应该使用SqlCommand-SqlDataReader还是SqlDataAdapter-DataSet。哪种方法更好?
发布于 2011-12-27 12:06:35
这在很大程度上取决于你想要的操作类型。
以下是我的建议。
,
打开performance.
- If you want to read faster and use benefit of Disconnected Arch. of ADO.net
- This will automatically close/open connection.
- Also it will also allow you to automatically handle update in DataSet back to DataBase. ( SqlCommandBuilder)
- This will give you better performance for insert and update.
如果你使用的是SQLFrame3.5、sp1或更高版本,我建议Linq to SQL或Entity Framework也能解决你的问题。
谢谢。
发布于 2011-12-27 12:20:31
SqlDataAdapter
insert/delete/update/select命令连接到你的数据库。所有来自SqlDataAdapter的好东西都是以更多的内存消耗为代价的。它通常用于需要多个用户连接到数据库的系统。
所以我要说,如果这不是你的情况,那就去买SqlCommand和互联模型吧。
发布于 2011-12-28 08:13:36
如果您只是读取数据,而不执行更新/插入/删除,那么SqlDataReader会更快。您还可以将其与DataSet结合使用。如果用using语句包装数据访问对象,运行时将为您处理连接清理逻辑。
我经常使用的同步访问模式是这样的:
DataTable result = new DataTable();
using (SqlConnection conn = new SqlConnection(MyConnectionString))
{
using (SqlCommand cmd = new SqlCommand(MyQueryText, conn))
{
// set CommandType, parameters and SqlDependency here if needed
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
result.Load(reader);
}
}
}对于更新/删除/插入,可能值得考虑使用SqlDataAdapter,但通常只有在DataSet中已经有数据的情况下。否则,有更快/更好的方法来做事情。
https://stackoverflow.com/questions/8640982
复制相似问题