2016-08-15 40 views
-1

我從第一數據庫由用戶選擇門票的第一列表和我從另一個數據庫 我想這兩個名單對比第二列表進行比較,以數據的列表,如果車票的標識是一樣的! 但我認爲在我的代碼中有什麼問題,任何人都可以幫助我嗎? the result is here in this picture從兩個數據庫

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      **strong text** 
      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\iharbahoui\Desktop\ConsoleApplication1\ConsoleApplication1\glpi.mdf;Integrated Security=True"); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("select * from glpi_tickets where user_id_recipient =1", con); 
      DataTable dt = new DataTable(); 
      SqlDataAdapter adp = new SqlDataAdapter(cmd); 
      adp.Fill(dt); 

      if (dt.Rows.Count >0) 
      { 
       //créér une planification du ticket dans l'agenda de la ressource 
       SqlConnection cnx = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\iharbahoui\Desktop\ConsoleApplication1\ConsoleApplication1\AZPbd.mdf;Integrated Security=True"); 
       cnx.Open(); 
       SqlCommand req1 = new SqlCommand("select * from planification where user_id =1",cnx); 
       DataTable tb = new DataTable(); 
       SqlDataAdapter adpt = new SqlDataAdapter(req1); 
       adpt.Fill(tb); 
       foreach (DataRow row1 in dt.Rows) 
       { 
        foreach(DataRow row2 in tb.Rows) 
        { 
         if (row1["Id"].Equals(row2["idTicket"])) 
         { 
          Console.WriteLine("ticket existe deja"); 
         } 
         else 
         { 
          Console.WriteLine("ticket n'existe"); 
         } 
        } 
       } 
      } 
      else 
      { 
       Console.WriteLine("Ticket n'existe pas"); 
      } 
      con.Close(); 
      Console.ReadLine(); 
     } 
    } 
} 
+1

究竟你認爲是錯的?它似乎沒有崩潰。別忘了我們無法看到您的源數據。你是說它不會產生預期的結果?如果是這樣,你需要提供你期望的結果和一個數據樣本,然後任何人都可以實際發現問題。 – ADyson

+0

一步一步地調試你的代碼 - 在這種類型的循環中有這個消息是正常的。 – Aristos

回答

0

簡介:使用左外連接,而不是完全外部聯接。


的主要問題是,在內部循環foreachglpi_tickets.id你輸出比較結果與planification.idTicket從而導致成9行總滿笛卡兒積。

你真正想要的只是左外連接(檢查所有planification.idTicket項目,但每glpi_tickets.id只輸出一次)。以最小的改動你的代碼,這可以通過此修改來實現:

foreach (DataRow row1 in dt.Rows) 
{ 
    bool matchFound = false; 
    foreach (DataRow row2 in tb.Rows) 
     if (row1["Id"].Equals(row2["idTicket"])) 
     { 
      matchFound = true; 
      break; 
     } 
    Console.WriteLine(matchFound ? "ticket existe deja" : "ticket n'existe"); 
} 

或者,您可以執行外連接在服務器端的單一查詢。只留下一個(第一)連接,並在其內部使用全名是指第二個數據庫/表:

select ... 
from glpi_tickets t 
left outer join AZPbd.dbo.planification p 
... 

而且整個程序是這樣的:

static void Main(string[] args) 
{ 
    var connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\iharbahoui\Desktop\ConsoleApplication1\ConsoleApplication1\glpi.mdf;Integrated Security=True"; 
    using (var connection = new SqlConnection(connectionString)) 
    { 
     connection.Open(); 
     var query = @" 
      select t.id, 
       case 
        when (p.idTicket is null) then 'ticket n''existe' 
        else 'ticket existe deja' 
       end as ticket_exists 
      from glpi_tickets t 
      left outer join AZPbd.dbo.planification p 
       on t.id = p.idTicket 
       and p.user_id = @userId 
      where t.user_id_recipient = @userId"; 
     var command = new SqlCommand(query, connection); 
     command.Parameters.AddWithValue("@userId", 1); 
     var reader = command.ExecuteReader(); 
     var ticketsFound = false; 
     while (reader.Read()) 
     { 
      Console.WriteLine("{0}: {1}", reader["id"], reader["ticket_exists"]); 
      ticketsFound = true; 
     } 
     if (!ticketsFound) 
      Console.WriteLine("Ticket n'existe pas"); 
    } 
    Console.ReadKey(); 
} 
+0

認爲的幫助:)我發現更容易的另一種解決方案 – ibhar