I'm trying to get the SQL result in a C# string variable or string array. Is it possible? Do I need to use SqlDataReader in some way? I'm very new to C# functions and all, used to work in PHP, so please give a working example if you can (If relevant I can already connect and access the database, insert and select.. I just don't know how to store the result in a string variable).
相关问题
- Sorting 3 numbers without branching [closed]
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Graphics.DrawImage() - Throws out of memory except
- Difference between Types.INTEGER and Types.NULL in
You could use an SQL Data Reader:
Or you could use SqlCommand.ExecuteScalar()
Try This:
accessing the Emails from list
lstEmails[0]
->first emaillstEmails[1]
->second email...etc.,
This isn't the single greatest example in history, as if you don't return any rows from the database you'll end up with an exception, but if you want to use a stored procedure from the database, rather than running a
SELECT
statement straight from your code, then this will allow you to return a string:It can definitely be improved, but it would give you a starting point to work from if you want to go down a stored procedure route.
This May help you For MySQL
For SQL
Try this:
The
ExecuteScalar
function saves whatever type of data there is on your database - you just have to specify it.Keep in mind that it can only save one line at a time.