How to select the foreign key id in the main table?

Asked

Viewed 200 times

0

public List Select() { List ListaCaminhao = new List();

        SqlConnection conexao = new SqlConnection(strCon);

        string sql = "Select * from Caminhao;";

        SqlCommand cmd = new SqlCommand(sql, conexao);

        conexao.Open();

        try
        {
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                Model.Caminhao caminhao = new Model.Caminhao();

        caminhao.IDCaminhao = Convert.ToInt32(reader[0].ToString());

        caminhao.IDMotorista = convert.ToInt32(reader["IDMotorista"].ToString()); 

This second reader["IDMotorista"] is my secondary key, is the id I want to show the name.

  • 4

    Is the same question? http://answall.com/q/120793/91

2 answers

2

What you need is a JOIN, to obtain the name of the Driver on the secondary table.

Select C*, M.NomeMotorista from Caminhao C -- Corrija o M.NomeMotorista para seu campo
join Motorista M
on M.IDMotorista = C.IDMotorista  -- verifique o nome correto das chaves.

You will also need to change your class caminhao with another property Nomemotorista .

caminhao.NomeMotorista = convert.ToInt32(reader["NomeMotorista "].ToString());
  • continued appearing driver id still .. well I’ll think a little, thanks more for the help

  • you executed this query directly in the bank?

  • no, it was on the layer of my developer

  • as it was after the while (Read())

  • It’s the same, it hasn’t changed at all ..

  • Iddriver = Convert.Toint32(Reset["Iddriver"]. Tostring()); Listcaminhao.Add(caminhao); caminhao.name = Reader["name"]. Tostring(); caminhao.km = Convert.Toint32(Reader["km"]. Tostring(); caminhao.modelo = Reader["model"]. Tostring(); path.qtde_axis = Convert.Toint32(Reader["qtde_axis"]. Tostring(); footer.size = Convert.Toint32(Reset["size"]. Tostring());

  • catch { Console.Writeline("Failed to execute Truck selection ..."); } Finally { connected. Close(); } Return Listfooted; }

  • You added the driver name Reader and made the Join?

Show 3 more comments

1

Replace the line:

string sql = "Select * from Caminhao;";

For:

string sql = "SELECT c.*, m.nome AS nomemotorista FROM caminhao AS c
JOIN motorista AS m ON m.idmotorista = c.idmotorista";

In your 'Model.Caminhao' add also the attribute 'nameMotorista';

On the line:

 caminhao.IDMotorista = convert.ToInt32(reader["IDMotorista"].ToString()); 

Change to:

caminhao.nomeMotorista = convert.ToInt32(reader["nomemotorista"].ToString()); 

In addition, you should also change from 'walking.Iddriver' to 'walking.nomeMotorista' where you upload your list to the graphic element.

  • in my case the id is appearing as number in my gridviwer, I wanted to put my driver’s name in place of number.. understood, if it helps ..

  • Which columns of your 'Walk' table and your 'driver' table'?

  • table walking - idcaminhao, name, km, model qtde_eixo, size, idmotorista; table driver - idmotorista, name, age, Cpf, rg, address; are those there

  • Review the answer. I believe that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.