2011-08-10 68 views
0

我在數據庫中作爲3個表:檢索數據

  1. 的Emp(姓名,cellnumber,付款,Joining_date)
  2. Addmoney(姓名,date_of money_taken,類型)
  3. ADDDATE(姓名,Leaving_Date)

我希望只顯示這些從這些3表中的列(名稱,付款,Date_of_money_Taken,Joining_date,Leaving_date)所示,當用戶輸入存儲在EMP表在TextBox任何名稱,我在下面的代碼中遇到問題。這是引發此:

Exception({"The multi-part identifier \"name.Text\" could not be bound."})

private void ShowEmp_Load(object sender, EventArgs e) 
{ 
    // create the connection string 
    connectionString = GetConnectionString(); 
    connection = new SqlConnection(connectionString); 
    queryString = "select Emp.name,Emp.Payment,Emp.JoiningDate,Addm.Date,AddDate.LeavingDate from Emp,Addm,AddDate where name.Text='" + name + " ' ";// Select * From Emp 

    // create an SqlDataAdapter to execute the query 
    dAdapter = new SqlDataAdapter(queryString, connection); 

    // create a command builder 
    cBuilder = new SqlCommandBuilder(dAdapter); 

    // create a datatable to hold query results 
    dTable = new DataTable(); 

    // fill DataTable 
    dAdapter.Fill(dTable);<-EXCEPTION({"The multi-part identifier \"name.Text\" could not be bound."}) 

    // the DataGridView 
    //DataGridView dataGridView1 = new DataGridView(); 

    // BindingSource to sync DataTable and DataGridView 
    bSource = new BindingSource(); 

    // set the BindingSource DataSource 
    bSource.DataSource = dTable; 

    // set the DataGridView DataSource 
    dataGridView1.DataSource = bSource; 

} 
+0

我注意到,這種方法是所有內部的'加載'事件處理程序,所以它只會被加載調用一次,這是所需的效果還是你想查找發生在按鈕點擊或類似的東西? – Coops

回答

1

是不是你的錯查詢?嘗試此查詢:

queryString = " 
    select 
     Emp.name, Emp.Payment, Emp.JoiningDate, Addm.Date, AddDate.LeavingDate 
    from 
     Emp, Addm, AddDate where Emp.name = '" + name + "' "; 

你引用name.Text當它看起來像字段的名稱應該是Emp.name

請注意,你REALLY需要使用params來做到這一點。這是開放的SQL注入...

+0

但使用此查詢字符串後,我如何顯示datagridview中的數據,因爲它現在沒有在datagridview中顯示任何東西 – Anonynus

+0

@Anonynus您將需要弄清楚,因爲你如何連接結果可能有問題。即使沒有過濾,你可以讓網格顯示任何東西嗎?你確定名字中的文字是完全匹配嗎?也許你需要一個'LIKE'而不是'='來檢查你的SQL。有很多東西可能是錯的。做一些診斷和轉發。 – Kelsey