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

时间:2010-06-01 20:36:26

标签: c# .net visual-studio oop

我尝试编写一个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();
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

这是一个我从VB转换为C#(http://www.developerfusion.com/tools/convert/vb-to-csharp/)的简单函数。我广泛使用它。

简单的包装函数,用于帮助从现有连接返回数据集和SQL语句。 与每次通过连接字符串重新连接相比,这应该具有性能改进。将任何SQL错误包装成自定义格式。

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;

}

答案 1 :(得分:0)

   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;
    }