Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在使用Dataset时,使用()或不使用()进行Dispose时,哪个代码更优化_C#_Asp.net_Dataset_Webforms - Fatal编程技术网

C# 在使用Dataset时,使用()或不使用()进行Dispose时,哪个代码更优化

C# 在使用Dataset时,使用()或不使用()进行Dispose时,哪个代码更优化,c#,asp.net,dataset,webforms,C#,Asp.net,Dataset,Webforms,我在我的网站上使用了大量的Dataset,到目前为止,我一直在使用Dataset作为 string strSql = "SELECT * FROM Articles"; DataSet ds = new DataSet(); ds = DataProvider.Connect_Select(strSql); string Title = ds.Tables[0].Rows[0]["Article_Title"].ToString(); string Des

我在我的网站上使用了大量的
Dataset
,到目前为止,我一直在使用
Dataset
作为

    string strSql = "SELECT * FROM Articles";
    DataSet ds = new DataSet();
    ds = DataProvider.Connect_Select(strSql);
    string Title = ds.Tables[0].Rows[0]["Article_Title"].ToString();
    string Desc = ds.Tables[0].Rows[0]["Article_Desc"].ToString();
使用块进行编码

string strSql = "SELECT * FROM Articles";
    // Create a DataSet in using statement.
using (DataSet ds = new DataSet())
{
    ds = DataProvider.Connect_Select(strSql);
    string Title = ds.Tables[0].Rows[0]["Article_Title"].ToString();
    string Desc = ds.Tables[0].Rows[0]["Article_Desc"].ToString();
}
使用
DataSet

以上代码我使用的是
SQL
station,否则我使用
存储过程

只能确保在数据集上调用Dispose方法,即使发生异常。不确定优化了多少,但对于实现
IDisposable
接口的对象来说,这是一种更安全的方法和更好的实践。使用语句类似于try/finally块

就像:

DataSet ds;
try
{
ds = new DataSet();
ds = DataProvider.Connect_Select(strSql);
string Title = ds.Tables[0].Rows[0]["Article_Title"].ToString();
string Desc = ds.Tables[0].Rows[0]["Article_Desc"].ToString();
}
finally 
{
if(ds != null)
     ds.Dispose();
}

我建议你用整洁的。数据集效率很低。 从Nuget或Dapper.org下载Dapper

创建DTO(数据传输对象)

然后在数据层中创建一个基类来管理与数据库的连接

public abstract class SalesDb : IDisposable
{
    protected static IDbConnection OpenConnection()
    {
        IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NAME"].ConnectionString);
        connection.Open();
        return connection;
    }
}
public class ArticleService : SalesDb
{
    public IEnumerable<ArticleDto> SelectAll()
    {
        using (IDbConnection connection = OpenConnection())
        {
            var articles = connection.Query<ArticleDto>("SELECT * FROM Articles");

            return articles;
        }
    }
}
然后,创建从数据库返回数据的服务类

public abstract class SalesDb : IDisposable
{
    protected static IDbConnection OpenConnection()
    {
        IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NAME"].ConnectionString);
        connection.Open();
        return connection;
    }
}
public class ArticleService : SalesDb
{
    public IEnumerable<ArticleDto> SelectAll()
    {
        using (IDbConnection connection = OpenConnection())
        {
            var articles = connection.Query<ArticleDto>("SELECT * FROM Articles");

            return articles;
        }
    }
}
公共类ArticleService:SalesDb
{
公共IEnumerable SelectAll()
{
使用(IDbConnection=OpenConnection())
{
var articles=connection.Query(“从文章中选择*);
归还物品;
}
}
}

两者都是相同的。在第一条语句中,您需要包含System.Data的引用,而另一条语句则可以在没有它的情况下使用。@Dev:但哪一条语句优化得更好。“使用”是为了确保在超出范围后进行处置。但数据集是在内存数据库中管理的,无需自行处理。dotnet将负责处理您的对象。注意:如果此代码在函数中,则无法返回数据集。如果我不使用
ds.Dispose()
如果我必须使用它,它能保存吗?.Net不处理数据集。@KnowledgeSeek,如果对象实现了IDisposable,那么最好调用Dispose,不要让垃圾收集器清理它,这里有一个很好的讨论。