SQL Join多表查询

时间:2014-11-17 06:02:47

标签: c# sql join

我写了以下代码:

 cmd = new SqlCommand("Select City.City, Company.Company,Emp_Depart.Department,Emp_Name.Uname from Emp_Name INNER join Emp_Name on" +
            "Emp_Name.Id=Emp_Depart.Id Emp_Name.Id=Company.Id Emp_Name.Id=City.Id where Emp_Name.Id=" + txtId.Text);
 cmd.Connection = con;
 da = new SqlDataAdapter(cmd);
 dt = new DataTable();
 da.Fill(dt);
 txtName .Text = dt.Rows[0][1].ToString().Trim();

我收到以下错误

其他信息:Incorrect syntax near '.'.

我是Joins的新手 请帮帮我...

2 个答案:

答案 0 :(得分:2)

您错过了And operator

中的join condition
SELECT CITY.CITY,
       COMPANY.COMPANY,
       EMP_DEPART.DEPARTMENT,
       EMP_NAME.UNAME
FROM   EMP_NAME
       INNER JOIN EMP_DEPART
               ON EMP_NAME.ID = EMP_DEPART.ID
       INNER JOIN COMPANY
         ON EMP_NAME.ID = COMPANY.ID
       INNER JOIN CITY
         ON EMP_NAME.ID = CITY.ID 
WHERE EMP_NAME.ID =+ TXTID.TEXT

答案 1 :(得分:0)

您需要在多个连接条件之间添加AND。另外,在EMP_name on之后添加一个空格。