C# 如何使 sqlDataReader 脱机?

发布于 2024-12-12 02:17:25 字数 808 浏览 6 评论 0原文

由于未正确关闭连接,出现内存泄漏。这是由于使用全局函数访问数据库(使用不同的sql字符串),但我传回了sqldatareader。 我无法在方法中关闭它,也无法关闭与数据库的连接,因为它关闭了对数据的访问!并且它无法从该方法外部正确关闭。 :(

有什么方法可以获取 sqldatareader 授予离线访问权限的所需表。 这样我就可以关闭所有连接,但仍然可以访问该表。

注意,会返回不同的表所以存在不同的字段。我不想每次尝试连接时都必须重复代码,

private SqlDataReader OpenDataStream(String sql)
{
    SqlCommand sqlComm = new SqlCommand();
    sqlComm.Connection = new SqlConnection();
    sqlComm.Connection.ConnectionString = @"Myconnectionstring";
    sqlComm.CommandText = sql;
    sqlComm.Connection.Open();
    SqlDataReader data = null;
    data = sqlComm.ExecuteReader();

    return data;

    // Closing data here, or connection, results in returned object inaccessable.
}

或者可能是在方法之外关闭所有内容的有效工作方式(在访问了我需要的内容之后)?

I have a memory leak, due to not closing the connection properly. This is due to using a global function to access the database (with different sql strings), but I pass back an sqldatareader. I cant close this in the method, nor the connection to the DB, as it closes access to the data! And it doesnt close properly from outside this method. :(

Is there Anyway way I can take the desired table, that the sqldatareader grants access to, offline. So that I can close all the connections, but still access the table.

Note, Different tables are returned so different fields exist. I dont want to have to duplicate code each time I try and connect.

private SqlDataReader OpenDataStream(String sql)
{
    SqlCommand sqlComm = new SqlCommand();
    sqlComm.Connection = new SqlConnection();
    sqlComm.Connection.ConnectionString = @"Myconnectionstring";
    sqlComm.CommandText = sql;
    sqlComm.Connection.Open();
    SqlDataReader data = null;
    data = sqlComm.ExecuteReader();

    return data;

    // Closing data here, or connection, results in returned object inaccessable.
}

or maybe a valid working way of closing it all down outside the method (after I have accessed what I need)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

当爱已成负担 2024-12-19 02:17:25

尝试 DataTable.Load Method 方法:

private DataTable OpenDataStream(String sql)
{

    DataTable dt = new DataTable();

    SqlCommand sqlComm = new SqlCommand();
    sqlComm.Connection = new SqlConnection();
    sqlComm.Connection.ConnectionString = @"Myconnectionstring";
    sqlComm.CommandText = sql;
    sqlComm.Connection.Open();
    SqlDataReader data = null;
    data = sqlComm.ExecuteReader();

    dt.Load(data);

    data.Close();

    return dt;
}

Try the DataTable.Load Method method:

private DataTable OpenDataStream(String sql)
{

    DataTable dt = new DataTable();

    SqlCommand sqlComm = new SqlCommand();
    sqlComm.Connection = new SqlConnection();
    sqlComm.Connection.ConnectionString = @"Myconnectionstring";
    sqlComm.CommandText = sql;
    sqlComm.Connection.Open();
    SqlDataReader data = null;
    data = sqlComm.ExecuteReader();

    dt.Load(data);

    data.Close();

    return dt;
}
怀里藏娇 2024-12-19 02:17:25

您可以只返回 DataTable 而不是 SqlDataReader。这将用您的数据填充表,并且您可以在方法结束执行之前关闭连接。

private DataTable GetDataTable(String sql)
{
    SqlDataAdapter da = new SqlDataAdapter(sql, connection);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds.Tables[0];
}

本文可能值得一读。

You could just return a DataTable instead of a SqlDataReader. This will fill the table with your data and you can close the connections before the method ends its execution.

private DataTable GetDataTable(String sql)
{
    SqlDataAdapter da = new SqlDataAdapter(sql, connection);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds.Tables[0];
}

This might be worth a read.

泪是无色的血 2024-12-19 02:17:25

不要返回读取器,而是返回填充的 DataTable

Don't return the reader, return a populated DataTable instead.

旧情勿念 2024-12-19 02:17:25

您可以考虑使用诸如 DataSet 之类的东西来提供对数据的离线访问。这里有一些示例 - http://msdn.microsoft.com/en-us/ library/ms971499.aspx

此问题提供了多种从数据读取器中取出数据并将其存储在内存中的方法 - 如何轻松将 DataReader 转换为 List

You could consider using using something like a DataSet that provides off-line access to your data. Some examples here - http://msdn.microsoft.com/en-us/library/ms971499.aspx

or

This question provides a number of methods for taking the data out of a datareader and storing it in memory - How can I easily convert DataReader to List<T>?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文