tags:

views:

49

answers:

3

Hello, stack overflow !

I'm working on an application that manipulates SQL tables in a windows form application. Up until now, I've only been using the pre-generated Fill queries, and self-made update and delete queries (which return nothing).

I am interested in storing the value of a single value from a single column (an 'nchar(15)' name), and though I have easily written the SQL code to return that value to the application, I have no idea what it will be returned as.

SELECT [Contact First Name] FROM ContactSkillSet
WHERE [Contact ID] = @CurrentID

Can the result be stored directly as a string? Do I need to cast it? Invoke a toString method?

Thanks!

Edit: Interestingly enough, the following works:

String firstName = this.contactSkillSetTableAdapter.GrabFirstName(userKey);

The joys of the Visual Studio data wizard, I suppose.

A: 

A SQL statement that returns a single value is called a Scalar query. Normally Statements that use aggregate commands like COUNT, MAX, and MIN fall into this category. Other statements like your example are considered scalar as well.

Basically you can treat it like any other query. Check that there is at least one row, and if there is one, cast row zero column zero to the appropriate data type. If you are using Strongly Typed Data sets there are some features to make this easier, but it is not required to do so.

William Leader
+2  A: 

If you plan to return a single result from your query you can always use the ExecuteScalar method on a SqlCommand class and then cast it to a string (see example in first link, it casts it to an Int32, but same principle for Strings).

R0MANARMY
+2  A: 

A TableAdapter is used to fill a DataTable. The result of your example query will still be a DataTable but with a single column in a single row. You can get the value using:

var adapter = new SomeKindOfTableAdapter();
var dataTable = new DataTable();

adapter.Fill(dataTable);

string contactFirstName = (string)dataTable.Rows[0][0];

There are other ways to get your value without using a Table Adapter though:

string query = @"SELECT [Contact First Name] 
                FROM ContactSkillSet 
                WHERE [Contact ID] = @Current";

string result;

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    using(SqlCommand command = new SqlCommand(query, conn))
    {
        result = (string)command.ExecuteScalar();
    }
}
Justin Niessner
Technically you're right, but I don't know if I'd encourage this approach.
R0MANARMY
@R0MANARMY - I agree. I added another approach that I would prefer.
Justin Niessner
I already have the table adapter because I'm also using a listview to display some other data elsewhere in the form. Is there a particular reason one approach or the other wouldn't be encouraged? (ala @R0MANARMY)
Raven Dreamer
@Raven Dreamer: One approach shows intent better than the other. When you see `DataAdapter.Fill` you *usually* assume database is returning a collection of rows. When you see `ExecuteScalar` you *usually* assume you're getting a single value back. Makes it easier for the person who works on this code after you.
R0MANARMY
@R0MANARMY Ah, well if it's any consolation, I did not name the query method "Fill". Noted, and thank you.
Raven Dreamer
@Raven Dreamer: Doesn't matter to me, I don't have to maintain it :P
R0MANARMY