Click here to Skip to main content
Rate this: bad
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 20:43pm
Edited 25-Dec-12 23: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 at 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
0 OriginalGriff 300
1 Peter Leow 170
2 ProgramFOX 145
3 Zoltán Zörgő 130
4 Afzaal Ahmad Zeeshan 105
0 OriginalGriff 260
1 ProgramFOX 145
2 Peter Leow 130
3 Sergey Alexandrovich Kryukov 85
4 Fredrik Bornander 75

Advertise | Privacy | Mobile
Web02 | 2.8.150301.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2015
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