I want to get a single database SQL string

Related searches

I got a little problem with this:

readonly LoginMandant logon = new LoginMandant();
string query = "SELECT Name FROM tMandant WHERE Id_Mandant = " + userDetails.Id_Mandant + ";";

Session["MandantName"] = logon.Database.SqlQuery<string>(query).SingleOrDefaultAsync();

and it returns:

System.Threading.Tasks.Task`1[System.String]

But why? I wanted a single string like "test" in there.


Because you are using SingleOrDefaultAsync it will return a task object.

you should use SingleOrDefault() instead of async if you want to get the string from database synchronously.

below should work in your case:

Session["MandantName"] = logon.Database.SqlQuery<string>(query).SingleOrDefault();

Obtaining a Single Value from a Database, You may need to return database information that is simply a single value rather string connString) { Int32 newProdID = 0; string sql = "INSERT INTO Production .ProductCategory (Name) VALUES (@Name); " + "SELECT� To quickly get started with a single database, start with the Single database quickstart guide. To learn about migrating a SQL Server database to Azure, see Migrate to Azure SQL Database. For information about supported features, see Features.


you need to await operations that are task-like, i.e.

Session["MandantName"] = await logon.Database.SqlQuery<string>(query).SingleOrDefaultAsync();

however, this requires your code to be async; if it isn't, either make it async (but not async void), or look for a synchronous version of the API.

You should probably look into parameters, too; the code as shown is actively dangerous.

SQL Wildcard Characters, A wildcard character is used to substitute one or more characters in a string. WHERE CustomerName LIKE '_r%', Finds any values that have "r" in the second shows the complete "Customers" table from the Northwind sample database:� A single database has a defined set of compute, memory, IO, and storage resources using one of two purchasing models. When you create a single database, you also define a server to manage it and place it within Azure resource group in a specified region. The template used in this quickstart is from Azure Quickstart Templates.


Async will require you to await.

Session["MandantName"] = await logon.Database.SqlQuery<string>(query).SingleOrDefaultAsync();

However, the calling method also has to be an async (and its caller call await, and so on). I highly recommend this, however, if you are really short on time, you could do the SingleOrDefaultAsync().Result or simply call the SingleOrDefault(), however, it will block the execution until the database query completes.

IMPORTANT: Avoid string concatenation for the query string. You are risking SQL injection.

How to include a single quote in a SQL query, Unless you know how to "escape" a quote, your statement will have errors. In this video we'll walk you through how to include a single quote in a SQL query; we'll It's going to be set, let's do, select, to show the string, and then we'll execute it. Kris Wenzel has been working with databases over the past 28 years as a� Obtaining a Single Value from a Database. 03/30/2017; 2 minutes to read +7; In this article. You may need to return database information that is simply a single value rather than in the form of a table or data stream. For example, you may want to return the result of an aggregate function such as COUNT(*), SUM(Price), or AVG(Quantity).


Introduction to SQL Server's Common String Functions, This isn't a strict scale, and all functions have a use in some business case, but I The CHARINDEX function is used to find the position of one string within another. Kris Wenzel has been working with databases over the past 28 years as a� Re: Get Single Value From SQL Database [C#] May 28, 2014 08:27 AM | AidyF | LINK You could create a "User" or a "UserInfo" class that just has some basic essential data like their ID, name, username and email address and store that object in the session as well


Converting row values in a table to a single concatenated string , Converting row values in a table to a single concatenated string If I run this query then I get the result I want : The first part of the CTE is the anchor Link back to this article : https://www.sqlmatters.com/Articles/Converting row values in a What should be our practice to store comma separated list in Database System. I'd like to know how to select a single value from my MySQL table. The table includes columns username and id amongst others (id is auto-increment and username is unique). Given the username, I wan


Of course, I need a final single quote to delimit the entire text string. If you have this all munched together, sometimes it’s hard to read. So it’s just that at the end of the statement I can get away with adding space, and this kind of alerts you to the fact that this is for the SQL criteria, and then this is for the entire string.