0

I am using .net 3.5 . I have a database on which i want to run queries. I am using data adapters or SqlCommand to run the query. But eventually I want the data in row-wise and column-wise string data. What is the way to do that ? I mean how to extract the data in this way from the dataset(whuich is what is returned by sqlDataAdapter) ? Or is there some other approach to this problem ?

2 Answers 2

3

You should look into the DataSet class.

DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(
    queryString, connection);
adapter.Fill(dataset);

You access values by method of:

DataSet.Tables["tableName"].Rows[rowIndex]["columnName"]

Or alternatively:

foreach(DataTable table in dataSet.Tables)
{
    foreach(DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            //Do something with
            row[column];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Well, you could go the cheap way and use foreach to iterate through the Rows in the specified Table in the DataSet.

foreach(DataRow myRow in myDataSet.Tables[n].Rows) { ... }

Edit: Must have been typing this at the same time as the other guy :) You wanted string data. You'll probably have to use myRow[columnIndex].ToString() since it will be an Object type of some sort.

Also, if you just need one string from the database, may I suggest you write a query that does the concatenation server-side, and a wrapper function that gets myDataSet.Tables[0].Rows[0].ToString();

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.