2012-02-06 7 views
0

私は、テーブル全体を含む電子メールを送信するために使用しているアプリケーションがあります。その動的データを取得する方法はわかりません。私は確かに私はforループを使用する必要がありますが、私はデータの行を取得する方法を知らない。ここでMysqlデータを含むHTMLメールをアプリケーション内からテーブルに送信

は私がこれまで持っているものです。

msg.To.Add(selected);//put in custom account info 
     msg.Subject = "Message Subject"; 
     msg.IsBodyHtml = true; 
     msg.Body = "<html>"+ 
     "<body>"+ 
     "<center>"+ 
     "<h2>Your entire inventory:</h2>"+ 
     "<table>"+ 
     "<th>Product</th>"+ 
     //for each loop 

     "<th>Quantity</th>"+ 
     //for each loop 
      "</body>"+ 
      "</html>"; 

は、あなたが他のコードが必要な場合は、私に教えてください。

+0

製品情報のような音がデータベースから来ています。そのデータをプルするコードを表示できますか? – NotMe

+0

製品と数量のデータはどこから来ますか? – Stephen

+0

出力をどのように見えるかを知ることから始めてください。つまり、無効なHTMLを作成しています。 – Jacob

答えて

1

個人的には、文字列連結を使用してHTMLを構築することは避けています。コードを厄介なものにするだけでなく、出力をエンコードする際に偶発的な間違いを負う危険もあります。 1つの方法は、LINQ to XMLを使用してHTMLを構築する方法です。コードは次のようになります。

var html = new XDocument(
    new XElement("html", 
     new XElement("body", 
      new XElement("h2", "Your entire inventory:"), 
      new XElement("table", 
       new XElement("tr", 
        new XElement("th", "Product"), 
        from item in orders 
        select new XElement("td", item.ProductName)), 
       new XElement("tr", 
        new XElement("th", "Quantity"), 
        from item in orders 
        select new XElement("td", item.Quantity)))))); 

msg.Body = html.ToString(); 
+0

どのようなオブジェクトのタイプはあなたの例で 'アイテム'なので、私はそれをmysqlから引き出して、そのオブジェクトを製品と量にすることができますか?私はあなたの行くところを見ています。 –

+0

ProductNameとQuantityを表示しているだけの場合は、それらのプロパティを持つオブジェクトである必要があります。必要なデータが含まれているクエリを作成するだけです。 – Jacob

+0

これはレイアウトに優れていて、@ stephenの例はそれを得るのに偉大でした。どうもありがとう! –

1

すでにMySQL Connector for .NETがあるとします。正しい方向に進むためのコードがいくつかあります。 C#を使用してデータベースから読み込む方法がわからない場合は、それを読んでください。 MySQL Connector Documentation

必要なものを保持するための単純なProductクラス。

public class Product 
{ 
    public String ProductName { get; set; } 
    public Int32 Quantity { get; set; } 
} 

ここでは簡単なデータベースクラスの概要を示します。

public class DB 
{ 
    string connString = "Your Database Connection String"; 

    public List<Product> GetProductsAndQuantity() 
    { 
     string query = "SELECT product, quantity FROM yourTable"; 
     List<Product> prodList = new List<Product>(); 

     using (SqlConnection connection = new SqlConnection(connString)) 
     { 

      SqlCommand cmd = new SqlCommand(query, connection); 
      connection.Open(); 
      SqlDataReader reader = cmd.ExecuteReader(); 
      try 
      { 
       while (reader.Read()) 
       { 
        // Create new Product 
        Product p = new Product(); 
        p.ProductName = (String)reader[0]; 
        p.Quantity = (Int32)reader[1]; 
        // Add product to list 
        prodList.Add(p); 
       } 
      } 
      finally 
      { 
       reader.Close(); 
      } 
     } 

     return prodList; 
    } 
} 

これはあなたのメールを作成する場所です。

DB db = new DB(); 

List<Product> prodList = db.GetProductsAndQuantity(); 
      msg.Body... 
      msg.Body += " <table> <tr><th>Product</th> <th>Quantity</th></tr> "; 

      foreach (Product p in prodList) 
      { 
       msg.Body += "<tr><td>" + p.ProductName + "</td><td>" 
         + p.Quantity + "</td></tr>"; 
      } 

      msg.Body += "</table> "; 
+0

あなたの情報をmy mysql DBから取得し、@ jacobの例をレイアウトに使用しました。 –

関連する問題