2011-08-10 70 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