0

I need to retrieve the val from one column of a table, any record (the val should be the same at any given time in all records; that is to say, if it's "3", it will be "3" in all of them; if it's "17", it will be "17" in all of them (or "42" or whatever)).

So I saw legacy code like this:

DataSet ds = dbconn.getDataSet("SELECT siteNo FROM workTables");

foreach (DataRow row in ds.Tables[0].Rows)
{
    siteNum = row["siteNo"].ToString();
}

...which works, but seems kludgy and wasteful, as a gazillion records could be looped through, and only the value of the last loop is used.

So I tried to "cut to the chase" with this code:

String siteNum = ds.Tables[0].Rows.ToString();

...but got an exception because siteNum is System.Data.DataRowCollection

I then tried this:

String siteNum = ds.Tables[0].Rows[0].ToString();

...but got an exception because siteNum is System.Data.DataRow

So I finally reverted to the legacy kludgy code, pretty much, modifying it like so:

DataSet ds = dbconn.getDataSet("SELECT siteNo FROM workTables");

foreach (DataRow row in ds.Tables[0].Rows)
{
    siteNum = row["siteNo"].ToString();
    // just need the first one
    break;
}

This works, but I'm sure it's not "the preferred method." Who knows a "more better" way?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

1

Just change your query to SELECT TOP 1 siteNo FROM workTables This way you minimize processing on the server and network traffic.

user2669338
  • 175
  • 1
  • 2
  • 8
  • I'll try it, but I'm not sure that's supported in SQL CE. – B. Clay Shannon-B. Crow Raven Aug 20 '14 at 18:24
  • 1
    I haven't used SQL Server CE but I would imagine it's supported. If it's not then you could change TOP 1 to Max(siteNo), maybe the max function is supported? The right way to do it would be to have a stored procedure with one output parameter, are stored procs supported? If you must retrieve all the data and only get the first row just do it this way: string sitenum = ds.Tables[0].Rows[0]["siteNo"].ToString(); – user2669338 Aug 20 '14 at 18:35
  • No, no SPs; I don't think Max is supported, either. It's a very stripped-down subset of SQL; it should be called the Kardashian edition or something like that. – B. Clay Shannon-B. Crow Raven Aug 20 '14 at 19:17
  • 1
    Hopefully TOP 1 is supported. If it isn't you can just get the value from the first row like I showed before: string sitenum = ds.Tables[0].Rows[0]["siteNo"].ToString(); – user2669338 Aug 20 '14 at 19:31
  • I like your "ds.Tables[0].Rows[0]["siteNo"].ToString();" better than "Top 1"; that works. – B. Clay Shannon-B. Crow Raven Aug 20 '14 at 20:57
1

Use ExecuteScalar to return the first column of the first row in a result set:

var result = connection.ExecuteScalar("SELECT siteNo FROM workTables");
if((result != null) && (result != DBNull.Value))
{
    site = Convert.ToInt32(result);
}
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Probably worth at least experimenting to see if a TableDirect call is even faster, since it would avoid the query processor altogether. It would depend on the amount of data in the table. – ctacke Aug 22 '14 at 22:17