0

Hi I have three tables like this

////////////////////////////////////////////
PlaceCodename   |     DeviceConfig   |   DevInOut
----------      |     ------------   |   --------
Place_name      |     IP   (PK)      |   IP
Place_code (PK) |     Place_code     |   Action
                |     TypeofUseCode  |   Cardserial (PK)
                |     deevNum        |   date   (PK)
                                     |   time    (PK)

For each Cardserial there are some IPs which can be repeated. It mean the primary key in DevInOut is the combination of (date,time and Cardserial).

I want to give Cardserial and based on the IP ,get Place_code from DeviceConfig and based on that get Place_name from PlaceCodename table.

I wrote this query but it doesn't work:

SqlDataAdapter a = new SqlDataAdapter("select Place_code from DeviceConfig inner join        (select IP from DevInOut where Cardserial = '" + textBox12.Text + "') tb on
DeviceConfig.IP = tb.IP)", con);
SqlCommandBuilder comdBuilder = new SqlCommandBuilder(a);
DataTable t = new DataTable();
t.Locale = System.Globalization.CultureInfo.InvariantCulture;
a.Fill(t);
bindingSource1.DataSource = t; 
........

I even tried data table it didn't work either.How can I solve my problem?

4

2 回答 2

1

你说你想要,Place_name但你的代码显示你选择Place_code了。你只需要一个正常WHERE的来解决这个问题:

SqlDataAdapter a = new SqlDataAdapter(string.Format("SELECT Place_name FROM PlaceCodeName, DeviceConfig, DevInOut WHERE Cardserial = '{0}' AND PlaceCodeName.Place_code = DeviceConfig.Place_code AND DeviceConfig.IP = DevInOut.IP",textBox12.Text), con);
于 2013-08-25T04:04:55.053 回答
0
Select Place_Name 
From PlaceCodename  N 
     INNER JOIN DeviceConfig   D 
                 on N.Place_code = D.Place_code     
     INNER JOIN DevInOut IO 
                 ON D.IP = IO.IP 
WHere IO.Cardserial ='YOUSERIAL'
于 2013-08-25T04:21:05.030 回答