SQL/C# - 执行查询的最佳方法

2024-02-05

我需要从 C# 类中执行 sql 查询。我想到了2个选择

  1. 启动sqlcmd进程。
  2. 使用 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQL/C# - 执行查询的最佳方法 的相关文章