.net 如何从SQL中完美地返回数据集?

kknvjkwl  于 2023-08-08  发布在  .NET
关注(0)|答案(3)|浏览(92)

我试着写一个winform应用程序:
我不喜欢下面的代码:

DataTable dt = new DataTable();
                dt.Load(dr);
                ds = new DataSet();
                ds.Tables.Add(dt);

字符串

以上部分代码看起来不够,如何才能最好地加载数据集?

public class LoadDataset
    {
        public DataSet GetAllData(string sp)
        {
            return LoadSQL(sp);
        }
        private DataSet LoadSQL(string sp)
        {
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand(sp, con);
            DataSet ds;
            try
            {
                con.Open();

                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                ds = new DataSet();
                ds.Tables.Add(dt);
                return ds;
            }
            finally
            {
                con.Dispose();
                cmd.Dispose();
            }
        }
    }

4ktjp1zp

4ktjp1zp1#

下面是我从VB转换到C#的一个简单函数(http://www.developerfusion.com/tools/convert/vb-to-csharp/)。我广泛使用这个。
简单的 Package 函数,帮助通过现有连接从和SQL语句返回数据集。与每次通过连接字符串重新连接相比,这应该具有性能改进。将所有SQL错误 Package 为自定义格式。

public System.Data.DataSet GetDataSet(string sqlStatement, System.Data.SqlClient.SqlConnection connection)
{

System.Data.DataSet functionReturnValue = default(System.Data.DataSet);
if (connection == null) {
    throw new ArgumentNullException("connection");
}

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
System.Data.DataSet dset = new System.Data.DataSet();

try {
    //   Connect to the database
    if (connection.State != ConnectionState.Open) {
        connection.Open();
    }

    if (connection.State != ConnectionState.Open) {
        throw new MyCustomException("Connection currently {0} when it should be open.", connection.State));
    }

    //   Create a command connection
    cmd = new System.Data.SqlClient.SqlCommand();
    {
        cmd.Connection = connection;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlStatement;
    }
    //.ExecuteReader()        'Forward only Dataset

    //   Create a data adapter to store the inforamtion
    adp = new System.Data.SqlClient.SqlDataAdapter();
    dset = new DataSet();
    {
        adp.SelectCommand = cmd;
        adp.Fill(dset, "Results");
    }

    //   Return the resulting dataset to the calling application

    functionReturnValue = dset;
}
catch (System.Data.SqlClient.SqlException objSE) {
    functionReturnValue = null;
    //   Let the calling function known they stuffed up and give them the SQL to help out.
    throw new JDDataException(System.String.Format("SQL :- {0}.", sqlStatement), objSE);
}
finally {
    if ((cmd != null)) cmd = null; 
    if ((adp != null)) adp = null; 
    if ((dset != null)) dset = null; 
}
return functionReturnValue;

字符串
}

xytpbqjk

xytpbqjk2#

public string GetSqlConnection()
    {
        return  System.Configuration.ConfigurationManager.AppSettings["SqlConnectionString"];
    }

   public DataSet getDataSet(string sql)
    {
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(GetSqlConnection());
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        da.Fill(ds);
        conn.Close();
        conn.Dispose();
        da.Dispose();
        return ds;
    }

字符串

fafcakar

fafcakar3#

public static DataSet GetDs(string sql)
{
    try
    {
        string ConnectionString = ConfigurationManager.AppSettings["SqlConnectionString"];
        using (SqlConnection cn = new SqlConnection(ConnectionString))
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = new SqlCommand(sql, cn);
            da.Fill(ds);
            return ds;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("GetDs Error");
        Console.WriteLine("sql = " + sql);
        Console.WriteLine("Exception caught: " + ex.ToString());
        //SendErrorMessage("Error : " + ex.ToString());

        return null;
    }
}

字符串

相关问题