Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
AppendFormat
Message
 
 
À
08/02/2013 16:31:37
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01565543
Message ID:
01565580
Vues:
32
>StringBuilder only gives you gains if you are concatenating the same string over multiple lines of code. If you care creating the string in a single line, StringBuilder just gets in the way.
>
In my case I'm creating a long string, e.g.
DataSet dataSet;

                    if (database.ExecuteSqlCommand(sqlCommand, out dataSet, ref messageText, ref statusCode))
                    {
                        String description = "";
                        StringBuilder displayString = new StringBuilder();
                        String location;

                        if (0 == dataSet.Tables[0].Rows.Count)
                        {
                            description = tcDCI;
                            displayString.AppendFormat("Quantities On Hand For: {0}{1}   No On Hand quantities found.{1}", description, Environment.NewLine);
                        }
                        else
                        {
                            Int32 counter = 1;
                            Int32 onHand;

                            foreach (DataRow row in dataSet.Tables[0].Rows)
                            {
                                if (1 == counter)
                                {
                                    String matrixAttribute;
                                    matrixAttribute = (String)row["mattrib"];
                                    if (!String.IsNullOrWhiteSpace(matrixAttribute))
                                        matrixAttribute = ", (" + matrixAttribute + ")";
                                    description = ((String)row["idescrip"]).Trim() + matrixAttribute;
                                    displayString.AppendFormat("Quantities On Hand For: {0}{1}", description, Environment.NewLine);
                                    // This is a constant, so AppendLine would be appropriate
                                    displayString.AppendLine("  Location      Quantity");
                                    displayString.AppendLine("  ---------- -----------");
                                }
                                counter++;
                                location = (String)row["location"];
                                onHand = Convert.ToInt32(row["on_hand"]);
                                displayString.AppendFormat("  {0,-10} {1,11:N0}{2}", location, onHand, Environment.NewLine);
                            }
                        }
                        displayString.AppendFormat("{1}Quantities On Order For: {0}{1}", description, Environment.NewLine);
                        if (0 == dataSet.Tables[1].Rows.Count)
                        {

                            displayString.AppendFormat("  No On Order quantities found.{0}", Environment.NewLine);
                        }
                        else
                        {
                            displayString.AppendLine("  Location   PO Number        Ordered Received Pending Expected   Primary Vendor");
                            displayString.AppendLine("  ---------- ---------------- ------- -------- ------- ---------- -------------------------");
                            String vendor, externalPO, cExpected;
                            Int32 onOrder, received, remained;
                            DateTime expected;

                            foreach (DataRow row in dataSet.Tables[1].Rows)
                            {
                                {
                                    location = (String)row["location"];
                                    vendor = (String)row["vendor"];
                                    onOrder = Convert.ToInt32(row["qty_orderd"]);
                                    received = Convert.ToInt32(row["qty_rcvd"]);
                                    remained = Convert.ToInt32(row["qty_remain"]);
                                    expected = Convert.ToDateTime(row["date_expct"]);
                                    externalPO = (String)row["externl_po"];

                                    if (Siriusware.Library.Functions.DTSqlMinDate == expected)
                                        cExpected = "";
                                    else
                                        cExpected = expected.ToShortDateString();

                                    displayString.AppendFormat("  {0,-10} {1,-16} {2:N0} {3:N0} {4:N0} {5,-10} {6}{7}",
                                        location, externalPO, onOrder, received, remained, cExpected, vendor, Environment.NewLine);
                                }
                            }
                            
                        }
                        messageText = displayString.ToString();
                        Logging.Log(messageText, 3);
                    }
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform