2017-01-08 10 views
3

個々のテーブルから取得する必要がありますが、同じ列名からstring arrayに値を取得する必要があります。私は私が持っているテーブルの名前は何もありませんが。私は彼らがすべて列名「電子メール」を持っていることだけを知っています。C#で特定の列から文字列配列にすべてのテーブルを選択する方法

テーブル名を知られていない
 string connectionString = "SERVER=********;PORT=****;DATABASE=********;UID=********;PASSWORD=******;"; 
     MySqlConnection conn = new MySqlConnection(connectionString); 
     MySqlCommand command = conn.CreateCommand(); 


     try 
     { 
     conn.Open(); 
     command.CommandText = "SHOW FULL TABLES FROM ExampleDataBase"; 
     } 


     catch 
     { 
      MessageBox.Show("error"); 
     } 
     MySqlDataReader reader; 
     reader = command.ExecuteReader(); 

     while (reader.Read()) 
     { 

      string usermail = reader.GetString("email"); 

      string[] mail = new String[] { usermail}; 
      foreach (string s in mail) 
      { 
       listboxMails.Items.Add("email: " + s) 
      } 

     } 

答えて

0

あなたがDBと連携良いスタートではありません...

まずlist of all table name who have the email field

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE COLUMN_NAME = 'email' 
     AND TABLE_SCHEMA='ExampleDataBase'; 

セカンドを取得するには、すべてのテーブル名とUNION要求を生成します

(SELECT email FROM %myTable1%) 
UNION 
(SELECT email FROM %myTable2%) 
-- ... 
0

名前がない場合、Linqのマッピングを使用するのが最も簡単かもしれません。参照System.Linq.Dataを追加してください...ここにコードは次のとおりです。

using System.Linq; 
    using System.Data; 
    using System.Data.Linq; 
    using System.Data.Linq.Mapping; 

    namespace ... 
    { 
      class Program 
      { 
       static void Main(string[] args) 
       {      
        DataContext dataContext = new DataContext(connectionString); 
        Table<Email> Emails = dataContext.GetTable<Email>(); 
        IQueryable<string> query = from e in Emails 
               select $"email: {e.email}"; 


       } 
      } 

      [Table] 
      public class Email 
      { 
       [Column(IsPrimaryKey = true)] 
       public string email; 
      } 
    } 

これは、列挙するための最も簡単な方法でなければなりません。私はこれが役立つことを願っています

+0

複数のアカウントで重複する可能性があるため、DBが主キー列をユーザーの電子メールアドレスに設定するとは思わないので、おそらく(IsPrimaryKey = true)は必要ありません。 –

関連する問題