Direct method from SQL command text to DataSet(从 SQL 命令文本到 DataSet 的直接方法)
问题描述
如果我有 sql 命令,获取 DataSet 的最直接途径是什么?
What is the most direct route to get a DataSet if I have a sql command?
string sqlCommand = "SELECT * FROM TABLE";
string connectionString = "blahblah";
DataSet = GetDataSet(sqlCommand,connectionString);
GetDataSet()
{
//...?
}
我从 SqlConnection
和 SqlCommand
开始,但我在 API 中看到的最接近的是 SqlCommand.ExecuteReader()
.使用这种方法,我需要获取 SqlDataReader
,然后手动将其转换为 DataSet
.我认为有更直接的途径来完成任务.
I started with SqlConnection
and SqlCommand
, but the closest thing I see in the API is SqlCommand.ExecuteReader()
. With this method, I'll need to get a SqlDataReader
and then convert this to a DataSet
manually. I figure there is a more direct route to accomplish the task.
如果更简单,DataTable
也将符合我的目标.
If easier, a DataTable
will also fit my goal.
推荐答案
public DataSet GetDataSet(string ConnectionString, string SQL)
{
SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
///conn.Open();
da.Fill(ds);
///conn.Close();
return ds;
}
这篇关于从 SQL 命令文本到 DataSet 的直接方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 SQL 命令文本到 DataSet 的直接方法


基础教程推荐
- 将数据集转换为列表 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01