3

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?

2
  • Don't use inline SQL like in the manner you are doing it, use parameterized SQL to avoid SQL injection attacks. Commented Jun 13, 2013 at 17:02
  • Please look at this page: msdn.microsoft.com/en-us/library/… Commented Jun 13, 2013 at 17:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.