从 SQL 命令文本到 DataSet 的直接方法 [英] Direct method from SQL command text to DataSet

查看:9
本文介绍了从 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()
{
   //...?
}

我从 SqlConnectionSqlCommand 开始,但我在 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 的直接方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆