我试着写一个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();
}
}
}
型
3条答案
按热度按时间4ktjp1zp1#
下面是我从VB转换到C#的一个简单函数(http://www.developerfusion.com/tools/convert/vb-to-csharp/)。我广泛使用这个。
简单的 Package 函数,帮助通过现有连接从和SQL语句返回数据集。与每次通过连接字符串重新连接相比,这应该具有性能改进。将所有SQL错误 Package 为自定义格式。
字符串
}
xytpbqjk2#
字符串
fafcakar3#
字符串