daidai
daidai

Reputation: 541

Binding DataGridView and SQL Table

I am new with this so i think it will be easy for you.I am making WFA application.I have DataGridView on my form and i want to insert SQL table in DataGridView.Here is my code:

OracleCommand cmd = new OracleCommand();
        OracleDataReader reader;
        cmd.Connection = conn;

        cmd.CommandText = "select * from rezervacija where korisnicko_ime_posetioca = '" + kip + "'";
        cmd.CommandType = CommandType.Text;
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {

        }

I already opened the connection so thats not a problem.What do i need to do while reader is reading so i could bind data's?

Upvotes: 3

Views: 713

Answers (1)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

Use an OracleDataAdapter like this:

OracleDataAdapter yourAdapter = new OracleDataAdapter();
OracleCommand command = new OracleCommand("select * from rezervacija where korisnicko_ime_posetioca = :kip", conn);

//Add your parameters like this to avoid Sql Injection attacks
command.Parameters.AddWithValue(":kip", kip);
yourAdapter.SelectCommand = command;
DataSet yourDataSet = new DataSet("RezervacijaData");

yourAdapter.Fill(yourDataSet, "rezervacija");

//Finally do the binding

yourDataGrid.SetDataBinding(yourDataSet, "Rezervacija");

This is the general idea. I'm not at my development machine so I haven't tested the code, but it should be fairly close.

Upvotes: 1

Related Questions