Click here to Skip to main content
12,892,729 members (47,453 online)
Rate this:
Please Sign up or sign in to vote.
var q = db.Execute("a select query");

Hi! I wana access the columns (and rows) obtained from this query (like q[0].column1). But when I store it in a var it is not possible. What kind of datatype or technique can I use? Is there a difference between single row output and multiple row output when doing this? Im working with sql server CE db (sdf file) in webmatrix.
Posted 25-Dec-12 19:43pm
Updated 25-Dec-12 22:47pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

if you are working in with c# :

using(SqlConnection con = new SqlConnection(connectionstring))
  SqlCommand cmd = new SqlCommand("SELECT * FROM tableName", con);
  SqlDataadapter adpt = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  DataColumn dclA = ds.Tables[0].Columns[0];
  // OR
  DataColumn dclB = ds.Tables[0].Columns["ColumnName"];
  DataRow drA = ds.Tables[0].Rows[0];
  string DataA = drA["ColumnName"].ToString();
  // OR
  string DataB = drA[0].ToString();

Try it...
AshishChaudha 27-Dec-12 23:41pm
my +5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Try this.


        SqlCommand command = new SqlCommand();
        command.Connection = sqlCon;
        command.CommandType = CommandType.Text; 
        command.CommandTimeout = 216000;
        command.CommandText = "Your Query";
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        string dataValue = dt.Rows[0]["Column_Name"]; // returns First Rows    

Hope it helps you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Refer the following links these may help you.

C# DataTable

These are the different ways that you can access the columns and rows.

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

The first problem in your question is the way you queries a Database object in Web Pages.
As you can see (Database Class[^]) there are three methods to execute a query:
Query, to obtain a list of rows as IEnumerable<object>;
QuerySingle, to obtain a single row as object;
QueryValue, to obtain a single scalar value.
The Execute method executes a non-query SQL statement, instead.
So, if you want to obtain a list of row you should query your database with this statement:
var q = db.Query("SELECT ... ");

and if you want to obtain your rows sequentially you should use this block of code:
@foreach(var row in q){

Otherwise, if you want to access your rows randomly, you should convert the IEnumerable to a List:
var q = db.Query("SELECT ... ").ToList();

and then you can use statements like

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100