我需要从 C# 类中执行 SQL 查询。我想到了 2 个选项
- 启动一个sqlcmd进程。
- 使用 SqlCommand 对象。
我的问题是哪种方法更好?重要的是该解决方案仅在短时间内保持与服务器的连接。
如果上述内容不好,我愿意接受其他想法。
提前致谢。
最佳答案
使用 SqlCommand。此代码只会在很短的时间内保持连接处于事件状态(只要您的查询是高性能的):
DataTable results = new DataTable();
using(SqlConnection conn = new SqlConnection(connString))
using(SqlCommand command = new SqlCommand(query, conn))
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
dataAdapter.Fill(results);
关于c# - SQL/C# - 执行查询的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3735531/