2013-11-23 87 views
2

我正在做一個發票計劃,我需要從我的數據庫中的2個表中準備,我在兩個表中都創建了一個名稱爲Invoice_No的關係聯盟,並且我打電話給兩者並從Invoice_No調用了我的發票數據,但是當我執行所有工作良好,輸入數據和當我按下 打印按鈕,它給我的錯誤不明確的列名Invoice_No後,給我一個解決方案Crystal報表模糊列名稱錯誤。

Cursor = Cursors.WaitCursor; 
      frmSalesinvoice frm = new frmSalesinvoice(); 
      invoice rpt = new invoice(); 
      //The report you created. 
      SqlConnection myConnection = default(SqlConnection); 
      SqlCommand MyCommand = new SqlCommand(); 
      SqlDataAdapter myDA = new SqlDataAdapter(); 
      DS_Invoice_all myDS = new DS_Invoice_all(); 
      myConnection = new SqlConnection(cs); 
      MyCommand.Connection = myConnection; 
      MyCommand.CommandText = "select * from Invoice_Info,Items_Soled where Items_Soled.Invoice_No=Invoice_Info.Invoice_No and Invoice_No= '" + textBoxInvoiceNo.Text + "'"; 
      MyCommand.CommandType = CommandType.Text; 
      myDA.SelectCommand = MyCommand; 
      myDA.Fill(myDS, "Invoice_Info"); 
      myDA.Fill(myDS, "Items_Soled"); 
      rpt.SetDataSource(myDS); 
      frm.crystalReportViewer1.ReportSource = rpt; 
      frm.Show(); 

回答

0

當你加入在一份聲明中2個或多個表發生此錯誤並有列在這些具有相同名稱的表中,並且在引用語句中的列時,您沒有在列名前添加表名。

Items_Sold.Invoice_No=Invoice_Info.Invoice_No 
+0

如何前綴是什麼? – Xeroth

+0

你說你有兩張桌子。我們將它們命名爲table1和table2。假設每個表都有一個字段。我們將其命名爲field1。所以你可以將它作爲前綴table1.field1和table2.field1。 –

0

您需要在這裏使用表名,因爲列在兩個表中都存在。 SQL Server正在越來越糊塗,你是指哪一列於:)

select * 
from Invoice_Info,Items_Soled 
where Items_Soled.Invoice_No=Invoice_Info.Invoice_No 
and [TableName].Invoice_No = ???? 

JOIN
一種更好的方式來達到同樣的結果將是使用帶有ON子句,像這樣

select * 
from Invoice_Info INNER JOIN Items_Soled 
ON Items_Soled.Invoice_No=Invoice_Info.Invoice_No 
WHERE [TableName].Invoice_No = ???? 
JOIN

編輯

Cursor = Cursors.WaitCursor; 
      frmSalesinvoice frm = new frmSalesinvoice(); 
      invoice rpt = new invoice(); 
      //The report you created. 
      SqlConnection myConnection = default(SqlConnection); 
      SqlCommand MyCommand = new SqlCommand(); 
      SqlDataAdapter myDA = new SqlDataAdapter(); 
      DS_Invoice_all myDS = new DS_Invoice_all(); 
      myConnection = new SqlConnection(cs); 
      MyCommand.Connection = myConnection; 
      MyCommand.CommandText = "select * from Invoice_Info INNER JOIN Items_Soled ON 
Items_Soled.Invoice_No=Invoice_Info.Invoice_No WHERE [TableName].Invoice_No = '" + textBoxInvoiceNo.Text + "'"; 
      MyCommand.CommandType = CommandType.Text; 
      myDA.SelectCommand = MyCommand; 
      myDA.Fill(myDS, "Invoice_Info"); 
      myDA.Fill(myDS, "Items_Soled"); 
      rpt.SetDataSource(myDS); 
      frm.crystalReportViewer1.ReportSource = rpt; 
      frm.Show(); 
+0

我想在哪裏使用這個。可以獲得更多關於此的信息 – Xeroth

+0

如果您在代碼中使用了sql查詢,請將其替換爲此查詢 –

+0

不能在sintax附近工作。 :( – Xeroth

1

你需要改變你的SQL查詢,所以你沒有任何重複的列名稱。您可以對列進行別名,以便獲得唯一的名稱。

例如,如果兩列都有一個名爲ID的字段,則應使它們唯一。

select 

info.Id As info_id, 
soled.Id As soled_id, 
--rest of your columns here with the table prefix 

from Invoice_Info info 
inner join Items_Soled soled 
on soled.Invoice_No=info.Invoice_No 
where info.Invoice_No= ' 

此外,你應該儘量避免using Old Style joins它們可以使查詢變得難以閱讀..