0

I've got two questions basically. I have been searching quite a bit. But I mainly find console applications that are so basic I understand them regarding SQL.

However -- I take user input from my view class and send It with a method to a connect class. I want the connect class to handle all the work with SQL. Now I have a working connection and I write a correct SQL statement. How do I return it to the view class and what would you recommend like a ListBox.

When we did this in java we got a resultset and translated it so it would fit in an jtable. I wonder how I can solve this in Visual Studio.

public void askSQL (string sqlQuestion)
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = sqlQuestion;
    cmd.Connection = connector;

    try
    {
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            MessageBox.Show("Du läser in data");   
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("Fel vid anslutningen!" + e);
    }
}

I currently have no problems with the code. The connection is working and I recieve multiple answers "Du läser in data" since there are multiple columns and rows in my table.

1
  • 1
    what do you want to achieve is unclear. you can return a SqlDataReader object to the caller or process the data in the method itself. It depends on what you need your method to do. Commented Feb 1, 2012 at 10:45

1 Answer 1

1

Have the function return DataTable object that will be populated with the database data:

public DataTable askSQL (string sqlQuestion)
{
    DataTable table = new DataTable();
    try
    {
        using (SqlDataAdapter adapter = new SqlDataAdapter(sqlQuestion, connector))
        {
            adapter.Fill(table);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("Fel vid anslutningen!" + e);
    }
    return table;
}

This will return something as close to table structure as possible.. the DataTable has Rows collection, each row with data of one record from database, and each DataRow has ItemArray collection with the field values.

For exampe, to access field called "Email" from the second row, have such code:

DataTable table = connect.askSQL("Select Email From Users Where UserId In (1, 2)");
string email = table.Rows[1]["Email"].ToString();

You can iterate over the rows with simple loop:

foreach (DataRow row in table.Rows)
{
    string email = row["Email"].ToString();
    MessageBox.Show("Current email: " + email);
}

Hope this is enough information. :)

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

4 Comments

Im trying to get your code to work. In what would you display the data from the dataTable?
The first code is now working. And my return is working aswell. However, I'm trying to get the foreach, and other codes to work in the view class. It does not recognize "table" although I've have at the bottom of the code "return table;"
@Thomas thanks, had bug indeed fixed now. Post the code you have that is not working (edit your original question with it and notify me here) and I'll try to see what went wrong.
@Thomas you can use a DataGridView control to display the Datatable returned in above answer: msdn.microsoft.com/en-us/library/…

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.