Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c# - StringBuilder inside large data set this time not store in memory stream inside throug exception out of memory exception

i save data base to csv file inside store record. whenewer i store 1000000 record this time not convert in byte array to memory strem inside. his time throw exception out of memory exception below code

con.Open();
SqlCommand cmd = new SqlCommand("select top 1000000 from temp_paresh", con);
var sb = new StringBuilder();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
    string csvseperator = ";";

    for (int i = 0; i < sdr.FieldCount; i++)
    {
        sb.Append(sdr.GetName(i));

        if (i < sdr.FieldCount - 1)
            sb.Append(csvseperator);
    }
    sb.AppendLine();

    while (sdr.Read())
    {
        sb.Append(Convert.ToString(sdr["rowid"]));
        sb.Append(csvseperator);
        sb.Append(Convert.ToString(sdr["rowname"]));
        sb.Append(csvseperator);
        sb.Append(Convert.ToString(sdr["rcount"]));
        sb.Append(csvseperator);
        sb.AppendLine();
    }
}
con.Close();
MemoryStream str = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sb.ToString()));

byte[] fileContent = str.ToArray();
str.Flush();
str.Close();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="" + ("paresh.csv") + """);
HttpContext.Current.Response.AddHeader("Content-Length", fileContent.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(fileContent);

for example MemoryStream(System.Text.Encoding.UTF8.GetBytes(sb.ToString())); i convert byte array to store in memory strem this time throu exception. memory out of stream.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Write directly to the response. Don't use a string builder because it uses up memory. You make matters worse and double up the memory usage by converting the string to a byte array with GetBytes(). You then waste even more memory writing the byte array to a memory stream. The you waste more memory converting the memory stream back to a byte array. You should also wrap your connection in a using statement and running your code inside of a try catch block.

Try this.

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="" + ("paresh.csv") + """);
    HttpContext.Current.Response.ContentType = "application/octet-stream";

    con.Open();
    SqlCommand cmd = new SqlCommand("select top 1000000 from temp_paresh", con);
    var sb = new StringBuilder();
    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
        string csvseperator = ";";

        // this is no good unless you are going to write all fields to the output stream.
        //for (int i = 0; i < sdr.FieldCount; i++)
        //{
        //    HttpContext.Current.Response.Write(sdr.GetName(i));

        //    if (i < sdr.FieldCount - 1)
        //        HttpContext.Current.Response.Write(csvseperator);
        //}
        //HttpContext.Current.Response.Write("
");

        HttpContext.Current.Response.Write("rowid;rowname;rcount
");
        // You also need to escape your data if it contains a ";"
        while (sdr.Read())
        {
            HttpContext.Current.Response.Write(Convert.ToString(sdr["rowid"]));
            HttpContext.Current.Response.Write(csvseperator);
            HttpContext.Current.Response.Write(Convert.ToString(sdr["rowname"]));
            HttpContext.Current.Response.Write(csvseperator);
            HttpContext.Current.Response.Write(Convert.ToString(sdr["rcount"]));

            HttpContext.Current.Response.Write("
");
        }
    }
    con.Close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...